Learn C# Scripting For Unity in 15 Minutes - Part 2
Summary
TLDRIn this video, Raja from Charger Games continues his 'C# in Unity' series, focusing on essential C# scripting techniques for game development. He demonstrates how to spawn game objects like a ball, manage spawn points, and trigger actions using keyboard and mouse inputs. Additionally, he explains how to use collision detection to change object colors and automate repeated object spawning with randomization. By the end, viewers will understand how to create dynamic interactions and spawn objects at random positions. The video is part of a series with more tutorials available via links in the description.
Takeaways
- đź Learn C# scripting for Unity game development in a short span.
- đ Part 2 of a series, check out the first part and other courses for more learning.
- đ” Understand how to spawn game objects in Unity using C# scripting.
- đ Create a 3D object (ball), add color and a Rigidbody component for physics.
- đ Set up a spawn point for the game object using an empty GameObject.
- đŸ Create a GameManager object and a GameController script to handle spawning.
- đ§ Write a function to instantiate the ball object at the spawn point's position and rotation.
- đ Save the ball as a prefab for easy reuse in Unity.
- âšïž Implement functionality to spawn the ball when the space key is pressed.
- đ±ïž Learn to spawn the ball at the position of a mouse click on the screen.
- đŻ Change the color of the ball upon collision with another object using a script.
Q & A
What is the main focus of this video?
-The video focuses on teaching C# scripting in Unity, specifically covering essential functionalities needed for game development within 15 minutes.
What is the first task demonstrated in the video?
-The first task is creating and spawning a game object (a sphere) in Unity, which is later used as a ball in the game.
How does the video explain adding color to the game object?
-The video explains how to create a material and apply it to the game object by dragging and dropping the material onto the object to change its color.
What is the purpose of adding a 'rigid body' component to the ball?
-Adding a 'rigid body' component enables the ball to behave according to physics, allowing it to fall when dropped into the game world.
How does the instructor demonstrate the spawning of the ball at a specific location?
-The instructor creates a spawn point by making an empty game object, assigns it a position, and then writes a script that spawns the ball at that location when the game starts.
How can the ball be spawned when pressing a specific key?
-The instructor modifies the script to spawn the ball when the space bar is pressed using `Input.GetKeyDown(KeyCode.Space)` inside the update function.
How does the video explain spawning the ball at the mouse click position?
-The video explains converting the mouse position from screen coordinates to world coordinates and using that position to spawn the ball at the clicked location using `Camera.main.ScreenToWorldPoint(mousePos)`.
What method is used to detect collision and change the ball's color?
-The method `OnCollisionEnter` is used to detect when the ball collides with another object, and the ball's color is changed using the `Renderer` component to make it red upon collision.
How is the ball set to spawn repeatedly in the game?
-The `InvokeRepeating` method is used in the `Start` function to repeatedly call the `SpawnBall` function at a regular interval, allowing balls to spawn continuously.
How does the instructor randomize the spawn position of the ball?
-The instructor creates random x and z values within a specified range and uses these to spawn the ball at random positions across the plane in the game.
Outlines
đź Unity C# Scripting Basics
Raja from Charger.Games introduces viewers to C# scripting in Unity, continuing from part 1 of his series. He covers spawning game objects, specifically a ball, and modifying its properties such as color and physics behavior with a Rigidbody component. The script 'Game Controller' is created to handle spawning, and the process of creating a prefab for the ball is detailed. The tutorial moves on to demonstrate how to spawn the ball at game start and on pressing the spacebar.
đ Spawning Objects on Mouse Click
The second paragraph delves into spawning objects at the position of a mouse click. It explains how to adjust the z-position of the mouse coordinates and convert screen coordinates to world coordinates for accurate object placement. A new script, 'Ball Controller', is introduced to change the color of the ball upon collision with a plane. Additionally, the use of 'InvokeRepeating' is taught to continuously spawn balls at set intervals without further user input.
đ° Randomizing Spawn Locations
In the final paragraph, Raja teaches how to randomize the spawn location of objects over a defined area. The focus is on creating a random x and z position for spawning within certain limits. The 'Game Controller' script is updated to include randomization, and the 'Start' function is modified to repeatedly call the spawn function at random positions. The video concludes with a prompt for viewers to apply these lessons to their own game projects and to explore more courses for in-depth learning.
Mindmap
Keywords
đĄC# Scripting
đĄUnity
đĄGame Object
đĄRigid Body
đĄPrefab
đĄInstantiate
đĄSpawn Point
đĄUpdate Function
đĄCollision
đĄInvoke Repeating
Highlights
Introduction to the video series focusing on C# scripting in Unity within 15 minutes, highlighting that this is part 2 of the series and advising viewers to check out part 1 for a complete understanding.
Demonstration of spawning a 3D object (a sphere) in Unity, including adding color and rigid body physics to the object, and the steps needed to create and apply materials in Unity.
Setting up a spawn point in Unity using an empty game object and assigning it a visible icon, then using this spawn point as a reference for object instantiation.
Explanation of creating a C# script named `GameController` to manage game logic, including public references to the ball object and the spawn point.
Implementation of the `SpawnBall` function using the `Instantiate` method to create a copy of the ball object at a specified position and rotation, emphasizing the use of `Quaternion.identity` for default rotation.
Creating a prefab from the ball object to allow for reuse and efficient object spawning, and updating the script to instantiate the ball from the prefab.
Triggering the spawn function in Unity's `Start` method to automatically spawn the ball when the game begins, and verifying that the functionality works as expected.
Modifying the script to spawn the ball using keyboard input (space key), utilizing Unity's `Input.GetKeyDown` method to handle user interactions.
Changing the script to spawn the ball at the mouse click position by converting screen coordinates to world coordinates using `Camera.main.ScreenToWorldPoint`, ensuring accurate placement in the game world.
Introduction of a `BallController` script to detect collisions using the `OnCollisionEnter` method and change the ball's color on impact, showcasing a dynamic reaction to game events.
Usage of `InvokeRepeating` in Unity to repeatedly call the `SpawnBall` function at specified intervals, enabling automated and consistent spawning without user input.
Adding randomization to the ball spawn positions by creating a random `Vector3` for the spawn point, using Unity's `Random.Range` method for generating values within specified boundaries.
Ensuring that multiple balls spawn at different positions on the plane by specifying maximum x and z values in the `GameManager` and dynamically adjusting the spawn points.
Optimization tip: Using prefabs and scripts to control object behavior and automate gameplay mechanics, demonstrating how to scale this technique for more complex projects.
End of the tutorial with a summary of techniques covered: object instantiation, keyboard and mouse input handling, collision detection, color changes, and automated spawning, encouraging viewers to explore further courses on C# and Unity for deeper learning.
Transcripts
hey everyone this is raja from charger
games and welcome back to this video
so in this video we're going to learn
about c sharp scripting in unity
in just 15 minutes now this is part 2 of
my c sharp in 15 minutes series
so if you haven't checked the first part
yet make sure to check that out
and you can check out all my other
courses and videos from the links in the
descriptions below
so with that being said we have a lot to
cover and
let's get started so in this video we're
going to learn about a lot of
useful c-sharp scripting functionalities
that we can use while building games in
unity
now we have to cover a lot of things
within this short time so we're going to
go really fast
so let's get started so first of all
we're going to learn how we can spawn a
game object
in unity so in order to spawn a game
object first of all i'm going to go
ahead and create a new 3d object
a sphere i'm going to rename this one to
ball now i'm going to add a color to it
so inside my materials i have this
colors
if you want to create a material you can
go to right click create
and new material from here this way
and i have this material here i'm going
to literally drag and drop it
right here so now as you can see our
ball has this color
now i'm gonna go and add a rigid body to
it so i'm gonna go to add component
physics and i'm gonna add a rigid body
component to it
now if you click on play
and if i drag my ball up as you can see
it falls down because now it has the
rigid body attached to it
so now what we're gonna do is whenever
our game starts we want to simply
spawn this ball we don't want it to be
here at the beginning but whenever we
start the game we have to spawn the ball
so in order to do that we also need a
spawn point where we actually want to
spawn our wall
so here i'm going to click on create an
empty game object
and i'm going to name this one spawn
point
and from the icons first of all let's go
ahead and
reset its position now from the icons
i'm going to select an icon for it
like this so that we can see where it is
and i'm going to drag and
move it upwards somewhere like this so
this is the point
from where our ball will spawn so this
is our spawn point
now here i'm gonna go ahead and create
an empty game object and i'm gonna name
this one
game manager and this is what we're
gonna do all our experiments on
in this video and here inside my scripts
folder i'm gonna right click
and create a new c sharp script and i'm
gonna name this one
game controller
alright
so game controller so i'm gonna select
my game manager and drag and drop my
game controller over it
now i'm gonna double click to open it in
visual studio
all right so now we have to spawn a
object in order to spawn an object first
of all we need to create
a public game object not game controller
public game object ball
so this ball object will store the ball
reference to our ball game object
next we need a public transform
spawn point so this is the position
where we are going to spawn our object
all right so now here
we're going to create a new function
we're going to call it void
spawn ball
and inside the spawnvolt function we're
going to spawn our ball
and for that we're gonna write
instantiate and what do we want to
instantiate
we want to instantiate the ball game
object and at which position
we want to associate at spawn point dot
position
and which rotation we want we want
quaternion
dot identity all right so we want to
spawn our ball at this position with
this rotation that means with zero
rotation
so now let's go ahead and save this
script go back to unity here in unity
before we can use it we need to save our
ball as a prefab so that we can reuse it
so inside my assets folder i'm gonna go
ahead and create a new folder
and i'm gonna call this one prefab now
inside the prefabs folder i'm gonna drag
and drop my ball right here
so now we can reuse this ball anytime
that you want so
i'm gonna go ahead and deactivate this
ball from the scene
then i'm gonna select my game manager
and as you can see it has a slot for
ball i'm gonna drag and drop my ball
from the prefab folder here
and for the spawn point i'm gonna drag
and drop the spawn point right here so
we have the ball and the spawn point
and now we need to open up game
controller and inside the start we need
to say
spawn ball so we are calling this
function in the start
now let's go back and now if i click on
play when the game starts
our ball spawns and anytime when i
restart the game
the ball keeps spawning just like this
so now let's learn how we can spawn the
ball when we press a button on our
keyboard
so let's open up our script and here
inside the update i'm going to say if
input dot get key down
key code dot space so that means
whenever we press the space button
we simply want to call the spawn ball
function
all right now i'm going to comment this
one out
and now whenever i press the space
button the ball will be spawned so let's
go back to unity
and every time i press the space button
as you can see a new ball spawns
and it keeps spawning like this so what
we can do is i can go to my plane and if
you don't have this plane you can simply
go to
create 3d object plane and create a new
plane
and i'm going to select my plane and
from the x rotation i'm going to give it
a rotation of let's say 10
or minus 10 all right just like this
so now you will see whenever i click on
play i press base button
and a new world spawns and it falls down
all right
so now we're gonna learn how we can
click our mouse on at a point
and spawn the ball at that position on
our screen so let's see how we can do
that so open up our script
and here inside the update function we
need to say if
input dot get mouse button down 0
that means if we are pressing the left
mouse button then first of all we need
to get the mouse position so here we're
going to say
vector vector3 mouse pose
equals input dot mouse position
all right next we need to do what we
need to do is we need to change the z
position of our mouse so into say mouse
mouse pose dot z equals
5 f all right or let's say 10 f
then we need to say vector 3 spawn
position so this is the position where
you're going to actually spawn
equals camera dot main dot
screen to world point and here we're
going to write
mouse pose so here we are converting our
mouse position which is in screen
coordinates
to this one that is world coordinate so
in order to use any position we need to
convert it to word coordinates otherwise
we cannot spawn the objects at the
correct position
so now that we have got the position we
need to spawn our ball at this position
so to do that i am going to simply copy
this spawn ball code
place it here and instead of this spawn
pointed position
here we're going to say spawn pose
all right so this is the spawn position
that we have calculated from our
calculations
so let's go back to unity so now you
will see whenever i click my mouse
the ball gets spawned at that point so
this is how we have learned how to
spawn a ball at any point where we click
our mouse
all right so now we're gonna learn how
to change the color of the balls when
they collide with this plane
so for that here we're gonna create
another new script we're going to call
this one
ball controller then i'm going to go to
my prefabs folder
select my ball go to add component
scripts and add the ball controller
script to it all right
now i'm going to double click on it now
here what we need to do is we need to
check when our ball is colliding with
the plane
for that we're going to call the void on
collision
enter function so this function gets
called automatically whenever our ball
collides
with any other object so whenever our
ball collides with any other object we
need to simply change the color of the
ball
in order to change the color of the ball
we're going to say get
component renderer and this will give us
access to the renderer component of our
ball
dot material dot color
equals color dot red so whenever our
ball collides with the plane or any
other game object
we can simply go and change its color to
color.red
you can simply go ahead and change it to
color.green or color.blue
i'm going to simply go ahead and make it
red so now let's save this and go back
to unity
now you will see i can spawn my balls at
any point and whenever they fall down
they actually become red
so now you can see i can spawn my balls
at any position and whenever they fall
down
they automatically change the color and
they become red so our code is working
okay so now we're gonna learn how to
call this spawn ball functions
repeatedly again and again
so that we don't have to do anything and
this function will get called again and
again and the balls will get spawned
again and again
to do that we're gonna use something
called invoke repeating
all right so here inside the start
function
we're going to write invoke repeating
which helps us to call any function
repeatedly so first of all within a
string
we need to we need to write the name of
the function exactly as it is
so i'm going to go ahead and select my
spawn ball
and copy this and here i'm going to
paste
spawn ball all right so now
after that for the second parameter we
need to write after how many seconds we
want to start calling
so let's say after one second we want to
start calling and then we need to
mention
after how many seconds we're going to
repeatedly call it so let's say here i
pass
2f so that means it will call this
function
every two seconds so after every two
seconds this invoke repeating function
will get called
and the balls will be instantiated so
this is the code we need to write to
repeatedly call this
function again and again so let's save
this and go back to unity
and here if i click on play
now you will see i'm not doing anything
i'm not clicking my mouse or
pressing my space button but all the
balls are falling again and again one by
one one after one
and they are just going down all right
so our code is working
but as you can see all of them are
actually spawning at the
same position so they are not changing
their position they are
all falling at the spawn point so now
we're gonna learn how we can actually
randomize the position
and spawn them at any point that we want
to so if you select our spawn point as
you can see this is positioned here
so we need to find a random point over
this plane
so what we can do is we can get a random
position of x between this
which is about 2.87 so let's say 2.60 so
let's say
minus 3 so we need to spawn it between
minus x minus 3 to positive 3 in the x
axis all right and for the
z axis we're going to say from positive
3
to negative 3 all right so
anywhere within this point we want to
spawn this object
okay so now we need to go back to our
game controller script and here
as you can see this is the spawn ball
and we need to find a way so that we can
randomly spawn this ball
so here inside the spawn volt function
what you can do
is float
random x so this would be the random x
spawn point
equals random dot range
so at which range we want to spawn our
value
or spawn our ball so for that here going
to create two new
variables public float
max x and public float
max z so this will be the points which
will which we will use to randomly spawn
this
so here we're going to write random
range minus
max x comma max x
and for the second value we're going to
write random
z equals random dot range
minus max z comma max
z so now this will return us a random
value
between these two values okay and we're
going to use that random value
to spawn our ball so here we're going to
simply copy this code
and paste it here and here we're going
to write
vector3 random
spawn pose this is a new variable equals
new vector3 and for the x value we're
going to write
random x for the y value we're going to
write 10
f and for the z value we're going to
write
random z that we have already calculated
so now what will happen is this will
create a random spawn position
and now inside the instantiate function
instead of this we need to simply write
random spawn pose
all right so this way it is creating a
random x position
it is creating a random z position and
this is creating a random vector3
position which is
taking the random x and z and finally we
are instantiating our ball
in this random spawn point and now as
you can see in our start function we are
repeatedly calling this spawn ball
function so every time this gets called
this function gets called and the ball
will be instantiated at a random
location
over our plane so let's save this and go
back to unity
and see how it's working so now if i
click on play
you will see after some time a random
ball is getting generated
but the balls are as you can see all
spawning at the same point
and that's because we have not mentioned
the maximum x and z positions yet
so let's go to game manager as you can
see max x and max c is empty
so here i'm gonna write three and here
i'm write three as well
so now if i click on play now you will
see
all the balls are falling down at a
random value over our plane
and all of them are falling down so this
is how our random code is working
so thank you so much for watching this
video i hope you really enjoyed and
learned a lot of new things
so go ahead and apply this in your own
games so if you want to learn more about
c-sharp and unity by building a lot of
example games like 2d endless runner 3d
endless runner
and all that kind of stuff you can check
out all my courses from the links in the
description below so you can go to the
description take the courses and learn a
lot so thanks so much for watching make
sure to subscribe and i'm going to see
you in the next videos
you
Voir Plus de Vidéos Connexes
Learn C# Scripting for Unity in 15 Minutes (2024 Working)
Learn C# Scripting for Unity 15 Minutes - Part 4 - Foreach Loop, Array, Find Tags
Godot 4 Crash Course #4 - Adding Meteors
HTML Tutorial #6: List | Web Development | Filipino | Tagalog
std::move and the Move Assignment Operator in C++
Mengenal Software 3D Blender - Bagian 2
5.0 / 5 (0 votes)