Learn C# Scripting for Unity in 15 Minutes (2020)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello this is Raja from charger games and welcome back to another new video so in this video we're gonna learn about si shop scripting in unity in just 10 to 15 minutes so this is gonna be fun let's do this alright so I'm gonna go really fast because we have to cover a lot of things in this short time so let's do this so here I have a 3d project opened and I have a cube right here and we're gonna do all our experiments on this cube only so here I have a scripts folder inside that I'm gonna right-click and create a new C sharp script I'm gonna simply name this one test then I'm gonna go ahead and select my cube and drag and drop the test script on the cube then double click to open it in Visual Studio so here our script has been opened so first things first we have these two functions created by default the start function and the update function the start function gets called at the start of the game when the game starts and the update function gets called again and again and again every single frame all right so the first thing we're gonna do is we're gonna learn to destroy the object so in order to destroy the object we have a function called destroy that you can call from anywhere so we're gonna call it from the start so we're gonna write destroy and what are you gonna destroy we can destroy this game object with which the script is attached so you can write game object exactly like this not in capitals game object in small capital like this so now we are calling it from the start so it should get destroyed as soon as the game starts so if I save the script and go back to unity now let's click on play and when the game starts as you can see the cube cut' destroyed all right so destroy function is working so let's open the script again now we're gonna learn to destroy it after a few seconds so to do that we're gonna write a comma and give the amount of time I want to destroy it after so let's say when we destroy up to three seconds I'm going to write 3 F and if you want to say float unit write F so we're gonna destroy it after three seconds so in unity we can play and then wait for 3 seconds 3 2 1 boom and as you can see in 3 seconds it get destroyed so that code is working alright so destroy code is working now let's learn how we can destroy it by clicking on it to do that here we're going to create a new function we can it void on Mouse down all right so this function gets called when we click on the object with our mouse alright so here what I'm gonna do is we're gonna comment this out by writing this double slash that means this code is not working anymore and we're gonna simply copy this paste it here and remove the 3f from here so now whenever we will click our mouse on the object or the cube it should get destroyed so let's save the script by pressing ctrl s and here let me go ahead click on play now let me click on it and as you can see as soon as I click on it the cube gets destroyed click and it goes destroyed so our mouse click is working alright so now let's learn how we can do that by using our keyboard inputs so in order to use keyboard inputs first of all let's learn how to take keyboard inputs to the keyboard inputs we need to use the input class okay and so we're gonna say if input dot get key down key code dot space all right so here we are pretty much saying that if we press the space button on our keyboard then what you want to do we want to do this going to destroy the object so let's copy it paste it right here all right so now our update will check every frame if we are pressing the space button and then it will destroy the game object now click on play and three two one space and it got destroyed so as soon as I press the space button it gets destroyed now let's learn to use some physics properties in the project so here I'm gonna select the cube go to add component and go to physics and from here I'm gonna add a rigidbody component so now it's part of unity physics engine and as you can see it uses gravity so it has gravity now so if I click on play and move the cube upwards as you can see it automatically falls down due to gravity so now we can use this rigid body and do anything we want with unities physics engine and here we're gonna create a new public variable not public just rigidbody RB and inside start begin a write our V equals get component rigidbody so here we are pretty much getting access to the rigidbody that we attached to our game object so now we can do anything that we want just by using this RB variable so in the update instead of destroying it let's add a force to our game object so here we gonna comment it out and now we're gonna write whenever we press the space button we want to say RB dot and force and what do you want to add force we want to add force in the upwards direction so for that we're gonna write vector three not up and how much you wonder at fourth let's say five hundred amount so we want to add a force of 500 amount in the upwards direction to our object whenever we press the space button so let's save this click on play and let's press the space button and as you can see as soon as I press the space but in our cube jumps because we are adding a force to it okay so now let's add a velocity to our cube to move it in a particular direction so in this case we can move our cube in the forward direction to do that what I'm gonna do is I'm gonna comment this cut out and now here I'm gonna insert the update function I'm going to simply say RB dot velocity so we are adding a velocity to a rigid body and we're gonna add a velocity in the forward direction so we're gonna say vector three dot forward I multiply it by let's say 100 F oh let's say 20 F so we are adding a velocity of 20 in the forward direction so we're gonna save the script so now if I click on play you will see how cube moves in the forward direction by this amount and as you can see it's already rotating so we don't want it to rotate so we gonna come to our cube come to the rigidbody and go to the constraints and click on freeze rotation on the x-direction all right so now if I click on play you will see our cube is not rotating anymore okay so now let's learn to detect collisions with other game objects in order to detect collisions we have a function void on collision enter ok and now inside this collision variable we have all the information about the object with which we have collided so here we gonna check if collision that game object dot tag equals enemy so that pretty much means if the object with which we have collided has an enemy tag attached then we can simply destroy our game object so this function gets called automatically whenever our game object gets collided with any other game object then it will check if the game object has an enemy tag attached then it will destroy our own game object so let's save this and go back to unity so here I'm gonna create a new 3d object sphere and I'm gonna put the sphere upwards then I'm gonna add a rigidbody to it physics rigidbody so now it has a gravity as well and it has this sphere Collider as well and now I'm gonna go to these tags click on app tag click on plus and add an enemy tag okay then again I'm gonna come to the sphere select the enemy tag from here so now it should detect collision so now if I click on play the sphere falls down it collides with the cube and before that our cue moves so so before testing this we need to comment out this line of code so now things should be fine so here we are back inside unity I'm gonna click on Play the sphere falls down onto the cube and as you can see as soon as it falls down the cube gets destroyed because the cube gets collided with an object which has the enemy tag attached so our collisions are working so now instead of destroying the cube we're gonna destroy the sphere whenever it collides with the cube so let's see how we can do that so here we are back inside our script so here we are checking if our object is colliding with an object which has the enemy tag attached then we are destroying our own game object so now we need to modify the code and instead of this let's copy this and paste it right here and comment out this line of code so now instead of writing game object we're gonna write collision daaad game object okay just by writing this simple code instead about cube now the sphere will get destroyed now let's click on play and three two one boom instead of destroying the cube this time the sphere gets destroyed because we have written the code in a different way okay so now let's learn to reload or load a new scene or new level in our game so if you go to file bill settings you can see currently we don't have any scenes in the build so let's click on add open scenes so our first scene isn't the build we need to create another new scene to learn this now we're gonna go to a file new scene then press ctrl s to save this and you can save it inside the scenes folder and we're gonna name this level 2 then click on save so now we have a new level now we're gonna go to the scenes folder and go back to our level go back to our game level and we're gonna open our script so here inside the script we're gonna say if we press the R then we're gonna go back to our next level okay to check that here we gonna say if input dot get key down key code dot R if we press the Add button then you want to reload the next level now in order to load the next level here we need to import a new thing or use a new name space we need to write using unity engine dot scene management ok so just by writing this code now we will be able to use all the scene management related functions so now here when to write scene manager not load scene and which scene we want to load with in quotations when it ride level 2 and then a semicolon so now whenever we press the our key our scene manager will load the level 2 level in our scene so let's save this and go back to unity now this is the most important step you need to go to file build settings and click on add open scenes we have only once in here let's go to our scenes and drag and drop the level 2 and put it here so both of these scenes should be here before we can actually load them so now if I click on play it goes down the queue gets destroyed I'm gonna press R and as soon as I press the are the new levels got loaded so as you can see this way we can load a new level just by pressing a key now let's go ahead and learn to display a text on the screen so here I'm gonna go to UI and create a new UI text element I'm gonna go to reset to reset its position at the center then I'm gonna go and change the text to you win I'm gonna go to go and select the direct selection tool and double-click on the text then I'm gonna make it then I'm gonna go to the 2d mode and make it bigger something like this then I'm gonna go and change the font size and make it big and then I'm gonna change the text color to white put it on the center something like this now we have our text let's name it to win text okay now let's go back to our script so here first of all we need to create a public variable public gameobject win text and now whenever our Spirit collides with the cube we want to we want to actually show the Windex so whenever our spirit collides with cube first of all you're gonna destroy the sphere and then I'm gonna say win text not set active to true so before starting the game we're gonna deactivate the win text and when the game starts and when it collides with the game object we're gonna set it to true so that it shows up on the screen so save these and go back to unity in the beginning we need to set the wind text and disable it select our cube go to the script as you can see it is waiting for a reference to the wind text we can a drag and drop our wind text right here so now it has got access to it now we can a click on play and as you can see when it will come and destroy it our wind text will be displayed so this way we know how to check collisions and whenever how to actually activate or deactivate an object when some event happens ok so the last thing we're gonna learn is how to move our cube left and right by pressing the left and right arrow keys so here we gonna create two new variables float X input float Z input and we gonna create another variable public float speed okay so now inside the update we gonna say X input equals input dot get axis horizontal and Z input equals input dot you get access vertical so we get a value from the horizontal axis when we press the left and right arrow keys we get a value from the vertical axis when we press that up and down arrow keys now here we gonna say our B dot add force and addforce in our three different directions so in the x-direction we can add a four sub X input fix input multiplied speed in the y direction we're not going to add any force so make 0 in the Z we're gonna add a 4 so we're gonna say the input multiply speed alright so now just by writing this simple code a force will be added in the X direction and in the Z direction to our cube game object whenever we press the left and right arrow keys so now save this go back to unity here we can select our cube and as you can see it is waiting for a speed value let's give the value of 2 for now let's go to click on play and if I press the left arrow key okay so first of all if I press the left arrow key our cube should move left so let's go ahead and turn up the value of the speed I'm gonna make it about hundred let's say now if I click on play you will see all these things happen now if I click on left our key moves to the left and it automatically rotates so in order to stop that we're going to go to our constraints and click on freeze rotation on x y&z three of these now if I click on play if i press left it moves to the left if i press right it moves to the right so now as you can see our cube 10 to the left and right and if you want you can go ahead and adjust the speed value I'm gonna make it 50 let's say now if I click on play it should work fine so now as you can see it gives a better player controller setting so now is as you can see I can move our cube left and right just by pressing the left and right arrow key on our keyboard so thank you so much for watching this video I hope you really enjoyed by learning c-sharp unity scripting in this short amount of time so if you want to learn more you can check out all my courses from the links in the description below and check out my other videos as well so thank you so much for watching this is Raja from charger games and I'm gonna see you in the next videos
Info
Channel: Charger Games
Views: 486,155
Rating: undefined out of 5
Keywords: unity c# scripting tutorial, c# for unity, c# tutorial, unity C# game development, c# game programming, learn c# for unity, unity 3D c# tutorial, c# in 10 minutes, unity c# in 15 minutes, charger games
Id: 9tMvzrqBUP8
Channel Id: undefined
Length: 15min 14sec (914 seconds)
Published: Tue May 05 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.