Learn C# Scripting For Unity in 15 Minutes - Part 2

Charger Games
1 Aug 202014:31

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

00:00

🎮 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.

05:01

🔄 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.

10:01

🎰 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

C# scripting refers to writing code in the C# programming language to control the behavior of elements within Unity, a popular game development engine. In the video, C# scripting is used to create functionalities such as spawning objects, handling inputs, and controlling game logic.

💡Unity

Unity is a game development engine that allows developers to build 2D and 3D games. In the video, Unity is used as the platform where C# scripts are applied to create and manipulate game objects like spheres (balls) and manage game components such as the game manager.

💡Game Object

A game object is any object within a Unity scene, such as a character, item, or environmental element. The video demonstrates how to create and manage a game object, specifically a sphere (ball), which can be manipulated through scripting to spawn, move, and change properties.

💡Rigid Body

A rigid body is a Unity component that enables physics-based behavior, such as gravity and collisions, for a game object. In the video, a rigid body is added to the sphere so that it can interact with the game world physically, allowing it to fall when the game starts.

💡Prefab

A prefab is a pre-configured game object in Unity that can be reused and instantiated multiple times in a scene. In the video, the ball object is saved as a prefab so that it can be spawned dynamically during the game whenever needed, rather than existing statically in the scene.

💡Instantiate

Instantiate is a function in Unity that creates a new instance of a game object. The video shows how this function is used to spawn balls at different points in the game, creating dynamic gameplay where objects appear based on user input or automatically over time.

💡Spawn Point

A spawn point is a designated location in a Unity scene where game objects can be created or 'spawned.' The video explains how to create an empty game object and set its position as a spawn point, from which new balls will appear during the gameplay.

💡Update Function

The Update function in Unity runs once per frame and is commonly used for checking player inputs or continuously updating game elements. In the video, the Update function is used to detect when the player presses a key (e.g., spacebar) or clicks the mouse to spawn new balls.

💡Collision

A collision in Unity refers to when two game objects with physical properties interact, such as when a ball hits the ground. In the video, the OnCollisionEnter function detects when the ball collides with another object, triggering a color change to indicate the collision event.

💡Invoke Repeating

Invoke Repeating is a Unity function that repeatedly calls a specified function at defined intervals. In the video, this function is used to automatically spawn new balls at regular time intervals, demonstrating how game mechanics can be automated through scripting.

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

play00:00

hey everyone this is raja from charger

play00:02

games and welcome back to this video

play00:04

so in this video we're going to learn

play00:06

about c sharp scripting in unity

play00:08

in just 15 minutes now this is part 2 of

play00:11

my c sharp in 15 minutes series

play00:13

so if you haven't checked the first part

play00:15

yet make sure to check that out

play00:16

and you can check out all my other

play00:18

courses and videos from the links in the

play00:20

descriptions below

play00:21

so with that being said we have a lot to

play00:24

cover and

play00:25

let's get started so in this video we're

play00:27

going to learn about a lot of

play00:29

useful c-sharp scripting functionalities

play00:32

that we can use while building games in

play00:34

unity

play00:34

now we have to cover a lot of things

play00:36

within this short time so we're going to

play00:37

go really fast

play00:38

so let's get started so first of all

play00:41

we're going to learn how we can spawn a

play00:43

game object

play00:44

in unity so in order to spawn a game

play00:46

object first of all i'm going to go

play00:47

ahead and create a new 3d object

play00:49

a sphere i'm going to rename this one to

play00:53

ball now i'm going to add a color to it

play00:55

so inside my materials i have this

play00:57

colors

play00:57

if you want to create a material you can

play00:59

go to right click create

play01:00

and new material from here this way

play01:04

and i have this material here i'm going

play01:05

to literally drag and drop it

play01:07

right here so now as you can see our

play01:09

ball has this color

play01:11

now i'm gonna go and add a rigid body to

play01:13

it so i'm gonna go to add component

play01:15

physics and i'm gonna add a rigid body

play01:17

component to it

play01:18

now if you click on play

play01:22

and if i drag my ball up as you can see

play01:24

it falls down because now it has the

play01:25

rigid body attached to it

play01:27

so now what we're gonna do is whenever

play01:29

our game starts we want to simply

play01:31

spawn this ball we don't want it to be

play01:34

here at the beginning but whenever we

play01:36

start the game we have to spawn the ball

play01:38

so in order to do that we also need a

play01:39

spawn point where we actually want to

play01:41

spawn our wall

play01:43

so here i'm going to click on create an

play01:45

empty game object

play01:46

and i'm going to name this one spawn

play01:48

point

play01:50

and from the icons first of all let's go

play01:52

ahead and

play01:53

reset its position now from the icons

play01:56

i'm going to select an icon for it

play01:58

like this so that we can see where it is

play02:00

and i'm going to drag and

play02:01

move it upwards somewhere like this so

play02:04

this is the point

play02:05

from where our ball will spawn so this

play02:07

is our spawn point

play02:09

now here i'm gonna go ahead and create

play02:10

an empty game object and i'm gonna name

play02:11

this one

play02:12

game manager and this is what we're

play02:15

gonna do all our experiments on

play02:17

in this video and here inside my scripts

play02:20

folder i'm gonna right click

play02:21

and create a new c sharp script and i'm

play02:23

gonna name this one

play02:25

game controller

play02:28

alright

play02:31

so game controller so i'm gonna select

play02:33

my game manager and drag and drop my

play02:35

game controller over it

play02:36

now i'm gonna double click to open it in

play02:38

visual studio

play02:39

all right so now we have to spawn a

play02:41

object in order to spawn an object first

play02:43

of all we need to create

play02:44

a public game object not game controller

play02:48

public game object ball

play02:51

so this ball object will store the ball

play02:54

reference to our ball game object

play02:56

next we need a public transform

play02:59

spawn point so this is the position

play03:02

where we are going to spawn our object

play03:04

all right so now here

play03:08

we're going to create a new function

play03:09

we're going to call it void

play03:11

spawn ball

play03:15

and inside the spawnvolt function we're

play03:16

going to spawn our ball

play03:18

and for that we're gonna write

play03:19

instantiate and what do we want to

play03:21

instantiate

play03:22

we want to instantiate the ball game

play03:23

object and at which position

play03:25

we want to associate at spawn point dot

play03:28

position

play03:29

and which rotation we want we want

play03:31

quaternion

play03:32

dot identity all right so we want to

play03:34

spawn our ball at this position with

play03:36

this rotation that means with zero

play03:37

rotation

play03:38

so now let's go ahead and save this

play03:39

script go back to unity here in unity

play03:42

before we can use it we need to save our

play03:44

ball as a prefab so that we can reuse it

play03:46

so inside my assets folder i'm gonna go

play03:47

ahead and create a new folder

play03:49

and i'm gonna call this one prefab now

play03:52

inside the prefabs folder i'm gonna drag

play03:53

and drop my ball right here

play03:55

so now we can reuse this ball anytime

play03:57

that you want so

play03:58

i'm gonna go ahead and deactivate this

play04:00

ball from the scene

play04:01

then i'm gonna select my game manager

play04:03

and as you can see it has a slot for

play04:05

ball i'm gonna drag and drop my ball

play04:07

from the prefab folder here

play04:08

and for the spawn point i'm gonna drag

play04:10

and drop the spawn point right here so

play04:11

we have the ball and the spawn point

play04:13

and now we need to open up game

play04:14

controller and inside the start we need

play04:16

to say

play04:17

spawn ball so we are calling this

play04:19

function in the start

play04:21

now let's go back and now if i click on

play04:22

play when the game starts

play04:24

our ball spawns and anytime when i

play04:26

restart the game

play04:27

the ball keeps spawning just like this

play04:30

so now let's learn how we can spawn the

play04:32

ball when we press a button on our

play04:33

keyboard

play04:34

so let's open up our script and here

play04:36

inside the update i'm going to say if

play04:38

input dot get key down

play04:42

key code dot space so that means

play04:44

whenever we press the space button

play04:46

we simply want to call the spawn ball

play04:48

function

play04:49

all right now i'm going to comment this

play04:51

one out

play04:52

and now whenever i press the space

play04:54

button the ball will be spawned so let's

play04:55

go back to unity

play04:57

and every time i press the space button

play04:58

as you can see a new ball spawns

play05:00

and it keeps spawning like this so what

play05:02

we can do is i can go to my plane and if

play05:04

you don't have this plane you can simply

play05:05

go to

play05:06

create 3d object plane and create a new

play05:08

plane

play05:09

and i'm going to select my plane and

play05:11

from the x rotation i'm going to give it

play05:13

a rotation of let's say 10

play05:14

or minus 10 all right just like this

play05:18

so now you will see whenever i click on

play05:20

play i press base button

play05:22

and a new world spawns and it falls down

play05:25

all right

play05:26

so now we're gonna learn how we can

play05:27

click our mouse on at a point

play05:29

and spawn the ball at that position on

play05:31

our screen so let's see how we can do

play05:32

that so open up our script

play05:36

and here inside the update function we

play05:38

need to say if

play05:40

input dot get mouse button down 0

play05:43

that means if we are pressing the left

play05:45

mouse button then first of all we need

play05:47

to get the mouse position so here we're

play05:48

going to say

play05:49

vector vector3 mouse pose

play05:53

equals input dot mouse position

play05:56

all right next we need to do what we

play05:58

need to do is we need to change the z

play06:00

position of our mouse so into say mouse

play06:02

mouse pose dot z equals

play06:06

5 f all right or let's say 10 f

play06:09

then we need to say vector 3 spawn

play06:12

position so this is the position where

play06:14

you're going to actually spawn

play06:15

equals camera dot main dot

play06:19

screen to world point and here we're

play06:21

going to write

play06:22

mouse pose so here we are converting our

play06:25

mouse position which is in screen

play06:27

coordinates

play06:27

to this one that is world coordinate so

play06:30

in order to use any position we need to

play06:32

convert it to word coordinates otherwise

play06:33

we cannot spawn the objects at the

play06:35

correct position

play06:36

so now that we have got the position we

play06:37

need to spawn our ball at this position

play06:40

so to do that i am going to simply copy

play06:42

this spawn ball code

play06:45

place it here and instead of this spawn

play06:48

pointed position

play06:49

here we're going to say spawn pose

play06:52

all right so this is the spawn position

play06:53

that we have calculated from our

play06:55

calculations

play06:56

so let's go back to unity so now you

play06:58

will see whenever i click my mouse

play07:00

the ball gets spawned at that point so

play07:02

this is how we have learned how to

play07:03

spawn a ball at any point where we click

play07:06

our mouse

play07:07

all right so now we're gonna learn how

play07:09

to change the color of the balls when

play07:10

they collide with this plane

play07:12

so for that here we're gonna create

play07:13

another new script we're going to call

play07:15

this one

play07:17

ball controller then i'm going to go to

play07:20

my prefabs folder

play07:22

select my ball go to add component

play07:25

scripts and add the ball controller

play07:27

script to it all right

play07:28

now i'm going to double click on it now

play07:31

here what we need to do is we need to

play07:32

check when our ball is colliding with

play07:34

the plane

play07:35

for that we're going to call the void on

play07:37

collision

play07:39

enter function so this function gets

play07:42

called automatically whenever our ball

play07:44

collides

play07:44

with any other object so whenever our

play07:46

ball collides with any other object we

play07:48

need to simply change the color of the

play07:50

ball

play07:50

in order to change the color of the ball

play07:52

we're going to say get

play07:53

component renderer and this will give us

play07:58

access to the renderer component of our

play08:00

ball

play08:01

dot material dot color

play08:04

equals color dot red so whenever our

play08:08

ball collides with the plane or any

play08:10

other game object

play08:10

we can simply go and change its color to

play08:13

color.red

play08:14

you can simply go ahead and change it to

play08:15

color.green or color.blue

play08:17

i'm going to simply go ahead and make it

play08:19

red so now let's save this and go back

play08:22

to unity

play08:23

now you will see i can spawn my balls at

play08:25

any point and whenever they fall down

play08:27

they actually become red

play08:32

so now you can see i can spawn my balls

play08:34

at any position and whenever they fall

play08:35

down

play08:36

they automatically change the color and

play08:38

they become red so our code is working

play08:41

okay so now we're gonna learn how to

play08:42

call this spawn ball functions

play08:44

repeatedly again and again

play08:46

so that we don't have to do anything and

play08:47

this function will get called again and

play08:48

again and the balls will get spawned

play08:50

again and again

play08:51

to do that we're gonna use something

play08:52

called invoke repeating

play08:54

all right so here inside the start

play08:56

function

play08:57

we're going to write invoke repeating

play09:01

which helps us to call any function

play09:02

repeatedly so first of all within a

play09:04

string

play09:05

we need to we need to write the name of

play09:07

the function exactly as it is

play09:09

so i'm going to go ahead and select my

play09:11

spawn ball

play09:12

and copy this and here i'm going to

play09:15

paste

play09:16

spawn ball all right so now

play09:19

after that for the second parameter we

play09:21

need to write after how many seconds we

play09:23

want to start calling

play09:24

so let's say after one second we want to

play09:26

start calling and then we need to

play09:27

mention

play09:28

after how many seconds we're going to

play09:30

repeatedly call it so let's say here i

play09:32

pass

play09:32

2f so that means it will call this

play09:34

function

play09:35

every two seconds so after every two

play09:37

seconds this invoke repeating function

play09:39

will get called

play09:40

and the balls will be instantiated so

play09:43

this is the code we need to write to

play09:44

repeatedly call this

play09:45

function again and again so let's save

play09:47

this and go back to unity

play09:49

and here if i click on play

play09:52

now you will see i'm not doing anything

play09:54

i'm not clicking my mouse or

play09:56

pressing my space button but all the

play09:58

balls are falling again and again one by

play09:59

one one after one

play10:00

and they are just going down all right

play10:03

so our code is working

play10:05

but as you can see all of them are

play10:07

actually spawning at the

play10:08

same position so they are not changing

play10:11

their position they are

play10:12

all falling at the spawn point so now

play10:15

we're gonna learn how we can actually

play10:16

randomize the position

play10:18

and spawn them at any point that we want

play10:19

to so if you select our spawn point as

play10:22

you can see this is positioned here

play10:23

so we need to find a random point over

play10:26

this plane

play10:27

so what we can do is we can get a random

play10:28

position of x between this

play10:31

which is about 2.87 so let's say 2.60 so

play10:34

let's say

play10:35

minus 3 so we need to spawn it between

play10:38

minus x minus 3 to positive 3 in the x

play10:41

axis all right and for the

play10:45

z axis we're going to say from positive

play10:48

3

play10:48

to negative 3 all right so

play10:52

anywhere within this point we want to

play10:54

spawn this object

play10:55

okay so now we need to go back to our

play10:59

game controller script and here

play11:02

as you can see this is the spawn ball

play11:04

and we need to find a way so that we can

play11:06

randomly spawn this ball

play11:08

so here inside the spawn volt function

play11:10

what you can do

play11:11

is float

play11:14

random x so this would be the random x

play11:17

spawn point

play11:18

equals random dot range

play11:21

so at which range we want to spawn our

play11:24

value

play11:24

or spawn our ball so for that here going

play11:27

to create two new

play11:28

variables public float

play11:31

max x and public float

play11:35

max z so this will be the points which

play11:38

will which we will use to randomly spawn

play11:40

this

play11:40

so here we're going to write random

play11:42

range minus

play11:44

max x comma max x

play11:47

and for the second value we're going to

play11:49

write random

play11:52

z equals random dot range

play11:56

minus max z comma max

play11:59

z so now this will return us a random

play12:02

value

play12:02

between these two values okay and we're

play12:04

going to use that random value

play12:06

to spawn our ball so here we're going to

play12:09

simply copy this code

play12:11

and paste it here and here we're going

play12:14

to write

play12:15

vector3 random

play12:18

spawn pose this is a new variable equals

play12:22

new vector3 and for the x value we're

play12:25

going to write

play12:25

random x for the y value we're going to

play12:28

write 10

play12:29

f and for the z value we're going to

play12:31

write

play12:32

random z that we have already calculated

play12:36

so now what will happen is this will

play12:38

create a random spawn position

play12:41

and now inside the instantiate function

play12:43

instead of this we need to simply write

play12:46

random spawn pose

play12:49

all right so this way it is creating a

play12:51

random x position

play12:52

it is creating a random z position and

play12:54

this is creating a random vector3

play12:56

position which is

play12:57

taking the random x and z and finally we

play13:00

are instantiating our ball

play13:02

in this random spawn point and now as

play13:04

you can see in our start function we are

play13:06

repeatedly calling this spawn ball

play13:07

function so every time this gets called

play13:10

this function gets called and the ball

play13:12

will be instantiated at a random

play13:14

location

play13:14

over our plane so let's save this and go

play13:17

back to unity

play13:18

and see how it's working so now if i

play13:20

click on play

play13:22

you will see after some time a random

play13:24

ball is getting generated

play13:26

but the balls are as you can see all

play13:27

spawning at the same point

play13:29

and that's because we have not mentioned

play13:30

the maximum x and z positions yet

play13:33

so let's go to game manager as you can

play13:34

see max x and max c is empty

play13:36

so here i'm gonna write three and here

play13:38

i'm write three as well

play13:39

so now if i click on play now you will

play13:42

see

play13:42

all the balls are falling down at a

play13:44

random value over our plane

play13:46

and all of them are falling down so this

play13:48

is how our random code is working

play13:51

so thank you so much for watching this

play13:52

video i hope you really enjoyed and

play13:54

learned a lot of new things

play13:55

so go ahead and apply this in your own

play13:57

games so if you want to learn more about

play13:59

c-sharp and unity by building a lot of

play14:01

example games like 2d endless runner 3d

play14:03

endless runner

play14:04

and all that kind of stuff you can check

play14:06

out all my courses from the links in the

play14:08

description below so you can go to the

play14:09

description take the courses and learn a

play14:12

lot so thanks so much for watching make

play14:13

sure to subscribe and i'm going to see

play14:14

you in the next videos

play14:31

you

Rate This

5.0 / 5 (0 votes)

Etiquetas Relacionadas
Unity scriptingC# tutorialGame developmentSpawning objectsRandom positionsKeyboard inputMouse inputRigid body physicsGame objectsUnity prefabs
¿Necesitas un resumen en inglés?