Learn C# Scripting for Unity in 15 Minutes (2024 Working)

Charger Games
5 May 202015:14

Summary

TLDRIn this Unity tutorial by Raja from Charger Games, viewers are guided through basic C# scripting techniques in Unity within 10-15 minutes. The video covers essential topics such as object destruction, keyboard and mouse input handling, physics integration with rigidbodies, collision detection, and loading new scenes. Additionally, it demonstrates how to display text on screen and control game objects using arrow keys. The tutorial is fast-paced, offering practical, step-by-step instructions to help users quickly grasp Unity scripting fundamentals.

Takeaways

  • 🚀 The video focuses on learning C# scripting in Unity within 10-15 minutes, using a 3D cube as the example.
  • 📦 Start by creating a C# script, attaching it to the cube, and learning the basics of the Start and Update functions.
  • 💥 The Destroy function can be used to remove the game object immediately or after a delay, such as 3 seconds.
  • 🖱️ The OnMouseDown function allows you to destroy the object when clicked with the mouse.
  • ⌨️ You can also use keyboard inputs, such as pressing the spacebar, to destroy the object using the Input.GetKeyDown function.
  • 🏀 Physics in Unity is demonstrated by adding a Rigidbody component to the cube, allowing it to react to gravity.
  • 💨 Rigidbody forces, like AddForce, can make the cube jump or move by pressing keys, adding velocity in specific directions.
  • 🔄 Collisions with other objects can be detected using the OnCollisionEnter function, which checks the object's tag and triggers actions like destroying an object.
  • 🎮 Scene management is introduced, where pressing a key like 'R' loads a new level or scene in Unity using SceneManager.
  • 📝 Text elements, such as displaying 'You Win' on collision events, are also covered using Unity's UI system and GameObject.SetActive.

Q & A

  • What is the purpose of the 'Start' and 'Update' functions in Unity scripts?

    -The 'Start' function is called at the start of the game when the game begins, while the 'Update' function is called repeatedly every single frame.

  • How can you destroy a game object in Unity using a script?

    -You can destroy a game object by calling the 'Destroy' function with the game object as its argument, typically within the 'Start' or 'Update' function.

  • What does the 'OnMouseDown' function do in Unity scripts?

    -The 'OnMouseDown' function is called when the object with the script attached is clicked with the mouse.

  • How can you make a game object respond to keyboard inputs in Unity?

    -You can use the 'Input.GetKeyDown' method to check if a specific key is pressed and then perform an action, such as destroying the object, in response.

  • What is a Rigidbody component and how does it affect a game object in Unity?

    -A Rigidbody component makes a game object part of Unity's physics engine, allowing it to have mass and be affected by gravity and other forces.

  • How can you add force to a game object in Unity to make it move?

    -You can add force to a game object by accessing its Rigidbody component and calling 'RB.AddForce' with a Vector3 direction and a force amount.

  • What does the 'OnCollisionEnter' function do in Unity scripts?

    -The 'OnCollisionEnter' function is called automatically when the game object collides with another game object, allowing you to perform actions based on the collision.

  • How can you detect if a game object has collided with a specific tag in Unity?

    -You can check the tag of the colliding object within the 'OnCollisionEnter' function using 'collision.gameObject.tag' and compare it to the desired tag.

  • What is the process to load a new scene in Unity when a certain condition is met?

    -To load a new scene, you can use the 'SceneManager.LoadScene' function with the scene name as its argument after ensuring the scene is added to the build settings.

  • How can you display text on the screen in Unity using UI?

    -You can create a UI Text element, position it, set the text properties, and then activate or deactivate it using script by setting the 'SetActive' property to true or false.

  • What is the role of the horizontal and vertical axis in controlling game object movement in Unity?

    -The horizontal axis typically corresponds to left and right arrow keys, while the vertical axis corresponds to up and down arrow keys. You can use these axis values to control the movement of a game object.

Outlines

00:00

🎮 Introduction to C# Scripting in Unity

Raja introduces the video, explaining that viewers will learn C# scripting in Unity in a short span of 10-15 minutes. He quickly walks through setting up a basic Unity project, where a cube object is selected for scripting experiments. The script includes two main functions: `Start` and `Update`, where the former is executed at the start of the game, and the latter runs every frame.

05:00

🛠️ Object Destruction and Time Delays

Raja demonstrates how to destroy a game object in Unity using the `Destroy` function. He shows how to set an object to be destroyed immediately when the game starts or after a specified time delay (e.g., 3 seconds). The tutorial covers how to modify the script to destroy the object either at game start or based on user input, such as mouse clicks and keyboard presses.

10:02

🚀 Applying Physics and Adding Forces

Raja explores Unity's physics engine, focusing on adding physical properties like gravity to a cube object by attaching a Rigidbody component. He walks through scripting the cube to react to user inputs, such as pressing the spacebar to apply a force that makes the cube jump upward. He also explains how to add velocity to the cube to move it in a specific direction and introduces collision detection by attaching an enemy tag to objects.

15:03

🌐 Scene Management and UI Elements

Raja covers Unity's scene management system by showing how to load different scenes (or levels) based on user input, such as pressing the 'R' key. He explains how to use UnityEngine's `SceneManagement` class to switch between scenes and demonstrates adding UI elements, like a win text that activates when certain conditions are met, such as collision detection between objects.

Mindmap

Keywords

💡C# Script

A C# script is a file written in the C# programming language, commonly used in Unity to control game objects and define behavior. In the video, the speaker creates a C# script called 'test' and attaches it to a cube object to demonstrate various actions, such as destroying the object or adding forces to it.

💡Unity

Unity is a popular game development platform that allows developers to build 2D and 3D games. The entire video is based on using Unity to create and manipulate game objects through scripting in C#, covering key concepts like physics, input handling, and scene management.

💡GameObject

A GameObject in Unity is the fundamental object in a game scene, representing characters, items, and other interactive elements. In the video, the speaker attaches the 'test' script to a cube GameObject to show how to manipulate and interact with it using various C# functions.

💡Destroy Function

The Destroy function in Unity is used to remove a GameObject from the scene, either immediately or after a specified time. The video demonstrates how to destroy a cube when the game starts, after a delay, or when clicking on the object using this function.

💡Start and Update Functions

These are two key Unity functions. The Start function is called once at the beginning of the game, and the Update function is called every frame during gameplay. The speaker explains how these functions are used to execute code, like destroying the cube at the game’s start or detecting input during gameplay.

💡RigidBody

A RigidBody is a component in Unity that allows GameObjects to interact with the physics engine. The video covers adding a RigidBody to the cube, which enables gravity and lets the cube fall, demonstrating how physics affects objects in Unity.

💡Input Class

The Input class in Unity is used to detect user input from devices like the keyboard or mouse. In the video, the speaker explains how to use this class to destroy the cube when pressing the spacebar or clicking on it, showcasing how user actions can influence game objects.

💡AddForce

AddForce is a Unity function that applies a force to a RigidBody, which can move or accelerate the GameObject. The video demonstrates how to use AddForce to make the cube jump when pressing the spacebar, by applying an upward force.

💡Collision Detection

Collision detection in Unity checks when two GameObjects with colliders come into contact. The speaker shows how to detect collisions between the cube and other objects like a sphere and how to execute actions (like destroying the sphere) when collisions occur.

💡Scene Management

Scene management refers to loading and unloading different game levels or scenes in Unity. The speaker explains how to use the SceneManager class to load a new level when the player presses the 'R' key, helping illustrate how to transition between different game environments.

Highlights

Introduction to Unity scripting in C# with a quick project setup.

Explaining the default 'Start' and 'Update' functions in Unity scripts.

Using the 'Destroy' function to remove objects in Unity, with a demonstration of immediate destruction upon game start.

Modifying the 'Destroy' function to delay object destruction after a specified number of seconds.

Implementing mouse click detection with the 'OnMouseDown' function to destroy an object when clicked.

Using keyboard input detection to destroy an object when the spacebar is pressed, using 'Input.GetKeyDown'.

Adding physics to the cube using a Rigidbody component, enabling gravity for realistic movement.

Adding an upward force to the cube when the spacebar is pressed, demonstrating basic physics manipulation with 'AddForce'.

Using Rigidbody velocity to move the cube in the forward direction continuously, with adjustments to prevent unwanted rotation.

Detecting collisions with the 'OnCollisionEnter' function, and destroying an object tagged as 'enemy' upon collision.

Demonstrating how to destroy the colliding object (a sphere) instead of the player’s object (the cube) upon collision.

Loading and switching between scenes with 'SceneManager.LoadScene' triggered by pressing the 'R' key.

Displaying a 'You Win' text element on the screen after a successful collision event using Unity's UI system.

Moving the cube left and right using arrow keys, adding force in the horizontal direction with 'AddForce' based on user input.

Wrapping up with suggestions for further learning and promoting additional Unity courses for deeper knowledge.

Transcripts

play00:00

hello this is Raja from charger games

play00:02

and welcome back to another new video so

play00:04

in this video we're gonna learn about si

play00:06

shop scripting in unity in just 10 to 15

play00:09

minutes so this is gonna be fun let's do

play00:11

this

play00:12

alright so I'm gonna go really fast

play00:14

because we have to cover a lot of things

play00:16

in this short time so let's do this so

play00:19

here I have a 3d project opened and I

play00:21

have a cube right here and we're gonna

play00:22

do all our experiments on this cube only

play00:24

so here I have a scripts folder inside

play00:27

that I'm gonna right-click and create a

play00:28

new C sharp script I'm gonna simply name

play00:30

this one test then I'm gonna go ahead

play00:33

and select my cube and drag and drop the

play00:36

test script on the cube then double

play00:38

click to open it in Visual Studio so

play00:39

here our script has been opened so first

play00:42

things first we have these two functions

play00:44

created by default the start function

play00:46

and the update function the start

play00:47

function gets called at the start of the

play00:49

game when the game starts and the update

play00:51

function gets called again and again and

play00:53

again every single frame all right so

play00:56

the first thing we're gonna do is we're

play00:57

gonna learn to destroy the object so in

play00:59

order to destroy the object we have a

play01:00

function called destroy that you can

play01:02

call from anywhere so we're gonna call

play01:04

it from the start so we're gonna write

play01:05

destroy and what are you gonna destroy

play01:07

we can destroy this game object with

play01:09

which the script is attached so you can

play01:12

write game object exactly like this not

play01:14

in capitals game object in small capital

play01:18

like this so now we are calling it from

play01:20

the start so it should get destroyed as

play01:22

soon as the game starts so if I save the

play01:25

script and go back to unity now let's

play01:27

click on play and when the game starts

play01:29

as you can see the cube cut' destroyed

play01:32

all right so destroy function is working

play01:34

so let's open the script again now we're

play01:36

gonna learn to destroy it after a few

play01:39

seconds so to do that we're gonna write

play01:41

a comma and give the amount of time I

play01:43

want to destroy it after so let's say

play01:44

when we destroy up to three seconds I'm

play01:46

going to write 3 F and if you want to

play01:49

say float unit write F so we're gonna

play01:52

destroy it after three seconds so in

play01:54

unity we can play and then wait for 3

play01:56

seconds 3 2 1 boom and as you can see in

play02:01

3 seconds it get destroyed so that code

play02:04

is working alright so destroy code is

play02:07

working now let's learn how we can

play02:09

destroy it by clicking on it to do that

play02:11

here we're going to create a new

play02:12

function we can

play02:13

it void on Mouse down all right so this

play02:18

function gets called when we click on

play02:20

the object with our mouse alright so

play02:23

here what I'm gonna do is we're gonna

play02:24

comment this out by writing this double

play02:26

slash that means this code is not

play02:28

working anymore

play02:29

and we're gonna simply copy this paste

play02:31

it here and remove the 3f from here so

play02:34

now whenever we will click our mouse on

play02:37

the object or the cube it should get

play02:39

destroyed so let's save the script by

play02:41

pressing ctrl s and here let me go ahead

play02:43

click on play now let me click on it and

play02:45

as you can see as soon as I click on it

play02:47

the cube gets destroyed click and it

play02:51

goes destroyed so our mouse click is

play02:53

working alright so now let's learn how

play02:55

we can do that by using our keyboard

play02:58

inputs so in order to use keyboard

play03:00

inputs

play03:00

first of all let's learn how to take

play03:02

keyboard inputs to the keyboard inputs

play03:04

we need to use the input class okay and

play03:07

so we're gonna say if input dot get key

play03:12

down key code dot space all right so

play03:19

here we are pretty much saying that if

play03:20

we press the space button on our

play03:23

keyboard then what you want to do we

play03:25

want to do this going to destroy the

play03:27

object so let's copy it paste it right

play03:29

here all right so now our update will

play03:32

check every frame if we are pressing the

play03:34

space button and then it will destroy

play03:36

the game object now click on play and

play03:38

three two one space and it got destroyed

play03:42

so as soon as I press the space button

play03:43

it gets destroyed now let's learn to use

play03:46

some physics properties in the project

play03:48

so here I'm gonna select the cube go to

play03:50

add component and go to physics and from

play03:53

here I'm gonna add a rigidbody component

play03:54

so now it's part of unity physics engine

play03:57

and as you can see it uses gravity so it

play03:59

has gravity now so if I click on play

play04:01

and move the cube upwards as you can see

play04:03

it automatically falls down due to

play04:04

gravity so now we can use this rigid

play04:06

body and do anything we want with

play04:08

unities physics engine and here we're

play04:10

gonna create a new public variable not

play04:13

public just rigidbody RB and inside

play04:17

start begin a write our V equals get

play04:21

component rigidbody so here we are

play04:24

pretty much getting access to the

play04:26

rigidbody that we

play04:27

attached to our game object so now we

play04:29

can do anything that we want just by

play04:31

using this RB variable so in the update

play04:34

instead of destroying it let's add a

play04:36

force to our game object so here we

play04:38

gonna comment it out and now we're gonna

play04:39

write whenever we press the space button

play04:42

we want to say RB dot and force and what

play04:47

do you want to add force we want to add

play04:48

force in the upwards direction so for

play04:51

that we're gonna write vector three not

play04:53

up and how much you wonder at fourth

play04:55

let's say five hundred amount so we want

play04:58

to add a force of 500 amount in the

play05:00

upwards direction to our object whenever

play05:02

we press the space button so let's save

play05:03

this click on play and let's press the

play05:06

space button and as you can see as soon

play05:08

as I press the space but in our cube

play05:11

jumps because we are adding a force to

play05:13

it okay so now let's add a velocity to

play05:15

our cube to move it in a particular

play05:17

direction so in this case we can move

play05:19

our cube in the forward direction to do

play05:21

that what I'm gonna do is I'm gonna

play05:23

comment this cut out and now here I'm

play05:25

gonna insert the update function I'm

play05:26

going to simply say RB dot velocity so

play05:29

we are adding a velocity to a rigid body

play05:31

and we're gonna add a velocity in the

play05:33

forward direction so we're gonna say

play05:34

vector three dot forward I multiply it

play05:36

by let's say 100 F oh let's say 20 F so

play05:42

we are adding a velocity of 20 in the

play05:44

forward direction so we're gonna save

play05:46

the script so now if I click on play you

play05:49

will see how cube moves in the forward

play05:50

direction by this amount and as you can

play05:52

see it's already rotating so we don't

play05:54

want it to rotate so we gonna come to

play05:56

our cube come to the rigidbody

play05:58

and go to the constraints and click on

play06:00

freeze rotation on the x-direction all

play06:02

right so now if I click on play you will

play06:04

see our cube is not rotating anymore

play06:06

okay so now let's learn to detect

play06:08

collisions with other game objects in

play06:10

order to detect collisions we have a

play06:12

function void on collision enter ok and

play06:17

now inside this collision variable we

play06:19

have all the information about the

play06:21

object with which we have collided so

play06:23

here we gonna check if collision that

play06:26

game object dot tag equals enemy so that

play06:30

pretty much means if the object with

play06:33

which we have collided has an enemy tag

play06:35

attached then we can simply destroy our

play06:38

game object so this

play06:40

function gets called automatically

play06:42

whenever our game object gets collided

play06:44

with any other game object then it will

play06:46

check if the game object has an enemy

play06:48

tag attached then it will destroy our

play06:50

own game object so let's save this and

play06:52

go back to unity so here I'm gonna

play06:54

create a new 3d object sphere and I'm

play06:58

gonna put the sphere upwards then I'm

play07:00

gonna add a rigidbody to it physics

play07:02

rigidbody so now it has a gravity as

play07:05

well and it has this sphere Collider as

play07:07

well and now I'm gonna go to these tags

play07:09

click on app tag click on plus and add

play07:13

an enemy tag okay then again I'm gonna

play07:16

come to the sphere select the enemy tag

play07:18

from here so now it should detect

play07:20

collision so now if I click on play the

play07:23

sphere falls down it collides with the

play07:25

cube and before that our cue moves so so

play07:28

before testing this we need to comment

play07:30

out this line of code so now things

play07:32

should be fine so here we are back

play07:34

inside unity I'm gonna click on Play the

play07:37

sphere falls down onto the cube and as

play07:39

you can see as soon as it falls down the

play07:41

cube gets destroyed because the cube

play07:43

gets collided with an object which has

play07:45

the enemy tag attached so our collisions

play07:47

are working so now instead of destroying

play07:50

the cube we're gonna destroy the sphere

play07:51

whenever it collides with the cube so

play07:53

let's see how we can do that so here we

play07:55

are back inside our script so here we

play07:57

are checking if our object is colliding

play07:59

with an object which has the enemy tag

play08:00

attached then we are destroying our own

play08:03

game object so now we need to modify the

play08:06

code and instead of this let's copy this

play08:09

and paste it right here and comment out

play08:13

this line of code so now instead of

play08:15

writing game object we're gonna write

play08:17

collision daaad game object okay just by

play08:22

writing this simple code instead about

play08:23

cube now the sphere will get destroyed

play08:26

now let's click on play and three two

play08:29

one boom instead of destroying the cube

play08:32

this time the sphere gets destroyed

play08:34

because we have written the code in a

play08:36

different way okay so now let's learn to

play08:39

reload or load a new scene or new level

play08:41

in our game so if you go to file bill

play08:43

settings you can see currently we don't

play08:45

have any scenes in the build so let's

play08:47

click on add open scenes so our first

play08:50

scene isn't the build we need to create

play08:51

another new scene to learn this

play08:53

now we're gonna go to a file new scene

play08:55

then press ctrl s to save this and you

play08:58

can save it inside the scenes folder and

play09:00

we're gonna name this level 2 then click

play09:03

on save

play09:04

so now we have a new level now we're

play09:06

gonna go to the scenes folder and go

play09:08

back to our level go back to our game

play09:10

level and we're gonna open our script so

play09:14

here inside the script we're gonna say

play09:15

if we press the R then we're gonna go

play09:18

back to our next level okay to check

play09:21

that here we gonna say if input dot get

play09:24

key down key code dot R if we press the

play09:30

Add button then you want to reload the

play09:32

next level now in order to load the next

play09:34

level here we need to import a new thing

play09:36

or use a new name space we need to write

play09:38

using unity engine dot scene management

play09:43

ok so just by writing this code now we

play09:45

will be able to use all the scene

play09:47

management related functions so now here

play09:50

when to write scene manager not load

play09:54

scene and which scene we want to load

play09:56

with in quotations when it ride level 2

play09:59

and then a semicolon so now whenever we

play10:02

press the our key our scene manager will

play10:04

load the level 2 level in our scene so

play10:06

let's save this and go back to unity now

play10:09

this is the most important step you need

play10:10

to go to file build settings and click

play10:13

on add open scenes we have only once in

play10:15

here let's go to our scenes and drag and

play10:17

drop the level 2 and put it here

play10:19

so both of these scenes should be here

play10:21

before we can actually load them so now

play10:24

if I click on play it goes down the

play10:28

queue gets destroyed I'm gonna press R

play10:29

and as soon as I press the are the new

play10:32

levels got loaded so as you can see this

play10:34

way we can load a new level just by

play10:36

pressing a key now let's go ahead and

play10:39

learn to display a text on the screen so

play10:41

here I'm gonna go to UI and create a new

play10:44

UI text element I'm gonna go to reset to

play10:47

reset its position at the center then

play10:50

I'm gonna go and change the text to you

play10:51

win I'm gonna go to go and select the

play10:55

direct selection tool and double-click

play10:57

on the text then I'm gonna make it then

play10:59

I'm gonna go to the 2d mode and make it

play11:01

bigger something like this then I'm

play11:05

gonna go and change the font size and

play11:06

make it big

play11:07

and then I'm gonna change the text color

play11:10

to white put it on the center something

play11:15

like this now we have our text let's

play11:17

name it to win text okay now let's go

play11:22

back to our script so here first of all

play11:24

we need to create a public variable

play11:25

public gameobject

play11:27

win text and now whenever our Spirit

play11:33

collides with the cube we want to we

play11:35

want to actually show the Windex

play11:37

so whenever our spirit collides with

play11:38

cube first of all you're gonna destroy

play11:40

the sphere and then I'm gonna say win

play11:42

text not set active to true so before

play11:46

starting the game we're gonna deactivate

play11:48

the win text and when the game starts

play11:50

and when it collides with the game

play11:52

object we're gonna set it to true so

play11:54

that it shows up on the screen so save

play11:56

these and go back to unity in the

play11:58

beginning we need to set the wind text

play12:00

and disable it select our cube go to the

play12:04

script as you can see it is waiting for

play12:05

a reference to the wind text we can a

play12:07

drag and drop our wind text right here

play12:10

so now it has got access to it now we

play12:13

can a click on play and as you can see

play12:16

when it will come and destroy it our

play12:19

wind text will be displayed so this way

play12:21

we know how to check collisions and

play12:22

whenever how to actually activate or

play12:25

deactivate an object when some event

play12:27

happens ok so the last thing we're gonna

play12:29

learn is how to move our cube left and

play12:32

right by pressing the left and right

play12:33

arrow keys so here we gonna create two

play12:35

new variables float X input float Z

play12:40

input and we gonna create another

play12:42

variable public float speed okay so now

play12:48

inside the update we gonna say X input

play12:51

equals input dot get axis horizontal and

play13:00

Z input equals input dot you get access

play13:07

vertical so we get a value from the

play13:10

horizontal axis when we press the left

play13:12

and right arrow keys we get a value from

play13:13

the vertical axis when we press that up

play13:15

and down arrow keys now here we gonna

play13:17

say our B dot add force and

play13:20

addforce in our three different

play13:22

directions so in the x-direction we can

play13:24

add a four sub X input fix input

play13:27

multiplied speed in the y direction

play13:30

we're not going to add any force so make

play13:32

0 in the Z we're gonna add a 4 so we're

play13:34

gonna say the input multiply speed

play13:37

alright

play13:38

so now just by writing this simple code

play13:40

a force will be added in the X direction

play13:43

and in the Z direction to our cube game

play13:46

object whenever we press the left and

play13:47

right arrow keys so now save this go

play13:50

back to unity here we can select our

play13:52

cube and as you can see it is waiting

play13:54

for a speed value let's give the value

play13:56

of 2 for now let's go to click on play

play14:00

and if I press the left arrow key okay

play14:04

so first of all if I press the left

play14:05

arrow key our cube should move left so

play14:08

let's go ahead and turn up the value of

play14:09

the speed I'm gonna make it about

play14:11

hundred let's say now if I click on play

play14:14

you will see all these things happen now

play14:16

if I click on left our key moves to the

play14:18

left and it automatically rotates so in

play14:21

order to stop that we're going to go to

play14:22

our constraints and click on freeze

play14:23

rotation on x y&z three of these now if

play14:27

I click on play if i press left it moves

play14:30

to the left if i press right it moves to

play14:32

the right so now as you can see our cube

play14:34

10 to the left and right and if you want

play14:35

you can go ahead and adjust the speed

play14:37

value I'm gonna make it 50 let's say now

play14:40

if I click on play it should work fine

play14:42

so now as you can see it gives a better

play14:45

player controller

play14:47

setting so now is as you can see I can

play14:50

move our cube left and right just by

play14:52

pressing the left and right arrow key on

play14:53

our keyboard so thank you so much for

play14:56

watching this video I hope you really

play14:57

enjoyed by learning c-sharp unity

play14:59

scripting in this short amount of time

play15:01

so if you want to learn more you can

play15:03

check out all my courses from the links

play15:05

in the description below and check out

play15:06

my other videos as well so thank you so

play15:08

much for watching this is Raja from

play15:10

charger games and I'm gonna see you in

play15:12

the next videos

Rate This

5.0 / 5 (0 votes)

Связанные теги
Unity scriptingC# tutorialgame developmentobject destructioncollision detectionphysics enginerigidbodykeyboard inputsscene managementbeginner guide
Вам нужно краткое изложение на английском?