Why to use Normalize in Unity ?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi there welcome in your encode lecture my name is philipp and in this video we'll be talking about normalized method so let's start open unity and one more thing if you would like to work on the same project as i'm working currently then open the link in the description of this video this will download your zip file unzip the file and let's add it into unity hub so click here add add this file and open the project and you should see the same project as i am working on if you still don't see it go to project assets open scenes and double click here sample scene okay so if we have the same project we can finally talk about normalize method okay let's play the game first so we'll see what's happening here so what we can do in the game we can just move okay so we can press the arrow keys or keys wasd and this will move the player in the movement of a player you will usually use this normalize normalize method because you can notice when you're moving along the x-axis along the horizontal axis you are moving the constant speed when you're going up and down you are moving constant speed then you are pressing the let's say right key and the up key at the same time so you're going in this diagonal direction you are moving much faster okay and that's the that's the thing with the normalized method it will provide you the constant speed at all directions but first we need to explain why we are moving the different speed at the diagonal direction okay let's pause the game let's go in the scene and let's click on the player let's see the current direction current position the player current position of the player is zero on the x and the z is a zero as well what i need to do if i would like to move on the one one direction on the one position i need to move x on the one and that on the one and this will move the player on the position one one okay so basically what i need to do i need to increase my i will draw it here okay so i need to increase my x direction that's from the previous lecture okay so i need to increase my x direction by one here is x set direction by one okay and then this will move me on the on the position one one right so that's how they are moving they are just increasing they are playing with their positions okay so why we are then moving faster when we are going diagonal direction first of all let's think what's happening when we are moving just on the one direction so let's say we are moving only on the x direction so let's say this is x one okay here is x one so what's the length of this vector what's the magnitude of this vector the magnitude of this lector is one okay so we are that's why we are moving the one one units per second when we are going just the up direction we are going up on the z axis again what's the length of this when we are moving this is one unit when i will press here that one again the length of this vector is one so that's why we are moving one unit per second but when we are moving the both directions so i'm moving here at the z and i'm moving the same on the x also okay so x here here is a z then the final vector so this is the position that i will move this final vector you can even see it straight from the i this is longer so this magnitude of this vector is longer okay we'll explain you this by by computing one equation soon but also let's debug log one thing let's click here assets if you have the same project a player controller here we have a movement vector and let's see this magnitude i was talking about so let's write here debug log and movement dot magnitude all right let's go to unity and let's play the game and we'll see what's the length of the vector what's the magnitude let's see console zero when they are not moving but i'm moving to the right the length of the vector is one when i'm moving the left the length of the vector is one up it's one down it's one when i'm moving the both directions up and right you can see the length of the vector will be larger so it's a one point four one four okay same it will happen whenever move the diagonally on the down direction one point four over four okay you can see this distance of the vector is larger it's uh the vector is has a bigger magnitude that's why you're moving faster at diagonal direction okay so when you will use here movement so vector 3 and it will use the function normalize this will normalize vector and your magnitude of the vector doesn't matter of which direction we will move there'll be always one so this will normalize this vector so they will play the game now the magnitude will be always one doesn't matter what direction i'm moving it's always one i'm sorry for interrupting this lecture but i would like to just remind you that if you are looking for the extensive course on how to build the games in unity then you can find such a course at ein code academy you are going to build three full-grown games in unity starting with the basic stuff and finishing with the fully feature rpg genre game where you can fight explore and complete quests so in the case you are interested you can check all the informations at academy.encode.com all right now let's explain this mathematically okay so let's go to open browser here is the grid system you can imagine this this view from up as we are seeing it here let's say the point here and we cannot just say but the point here is really zero zero on the x and the z so this is the start of the grid system here right okay so same same grid system i will show you now on the paper okay here so now let's imagine this direction we are moving right on the these x i will say here i will mark it as a x there is x and here is a z okay so we are moving one so here is the one here is the one here is our player at zero zero now we'll move on here okay we'll move up here one okay so the length of this vector is one one okay here is one here is one so that our final vector where we will move final position will be here right so i'll also try to write it straight but it's quite hard on the mouse okay so here is this one position one one okay now i will draw here this final vector and you can see straight from the eye this vector is longer and the magnitude is bigger s opposing to this or this upper one okay so how we can find the the distance how we can find the length of this vector okay you can see here there is a right triangle so i can mark this up side as a c how can you compute the c part you know this part right so this is the one length and there's also one this is the one okay so this is well one and we're looking for a c so you can see here straight away that is the pythagorean theorem so the c squared so hypotenuse equals a squared plus b square b squared so a squared plus b squared which is a c squared is equal one squared because a is one we can mark this as a and there's a b okay so plus 1 squared so c squared is equal to 1 plus 1 because i 1 squared is 1 so 1 plus 1 is 2. and then c we need to put a square root of the c so c is equal to the squared root of two okay so what is the square root of two now you can drag a calculator so it should be something bigger than one okay so okay let's open a calculator right so calculator and okay so okay second square of 2 square root of 2 is 1.404 okay this number is quite familiar right so it's a c is something around 1.404 and as the magnitude of this vector have we are moving diagonally before we are normalizing okay guys so that's what i showed you when we debug log this magnitude b4 we are normalizing so we'll comment it out again and that's the length or you could see when we are normalizing here before normalizing right so going diagonally 1.404 1.404 or something close to it at least okay all right so what is actually happening when we are normalizing let's debug log entire vector okay before normalizing so we'll debug log here entire vector okay so when we are moving this diagonal direction default normalizing so make sure the normalize is commented out or deleted so when we are moving this diagonal direction we are changing x and the z direction of the one so i will move upper right here upright you can see one one x and the z is a one okay okay then i will move down it will be minus values of course and so on when i will move to the right i'm just increasing the x value minus one and so on okay so depending on which side i'm going but we are interested in this diagonal movement up here where it's well one so that's the exact movement and i i have draw here so that's this movement one one okay let's see what will happen with this movement vector then we will normalize okay so i will uncomment normalize and here we'll provide this movement vector and i will see what will happen with the movement as i will be moving diagonally you will get different values so when i will move diagonally now the values of this vector will be 0.7 0.7 you could see it and moving diagonally 0.7 0.7 so this this normalized method what will do you it will squish the values of the x and the z position of the is that uh direction so our magnitude of the final vector is exactly one okay you can see it when we will draw it actually i will one more example so we'll be not moving now okay i'll remove all of this here actually everything so we could see that after after normalized we had the value 0.7 so it could be here and 0.7 here so we are moving straight here 0.7 0.7 up here all right like this okay perfect and now what's the our position when we're moving 0.7 0.7 okay somewhere here right so this is the vector we are looking for okay like this and we know that 0.7 and here is 0.7 and they are looking again for a c so now grab your calculators and compute it again so c squared equals 0.7 squared plus 0.7 squared it should be 2. 0.7 squared is 0.49 so c squared is 0.49 plus 0.49 right and that's c squared is equal that should be 0.90 and i can say okay that's that's close to the one so i can i can round it to the one okay so c squared is one so so a second the square root of the c then of the one is one so c is one okay so the length of this or this vector is then one so that's why we are using the normalize method this will squish the x part and the z part so our final magnitude is indeed one and we are moving the same speed at each direction okay so that's gonna be it guys so if you ever wondered why we need to use here where you're using the normalized function this will just squish your other parts of the vector so the final magnitude of the vector will be always one it doesn't matter in which direction you are you are moving so that's why we leave here normalize you can remove the debug lock and we'll be moving in the constant speed at each direction diagonal speed will be same as the speed when we are moving to the sides or up and down all right guys that should be it from this video if you're interested into more unity lectures check the channel if you like today's video please press the like button really helps me and i really appreciate it i hope to see you in my other videos and i have wish you a very nice day guys so cheers
Info
Channel: Eincode
Views: 16,354
Rating: undefined out of 5
Keywords: unity, game development, programming
Id: oCU8Ew1XTbs
Channel Id: undefined
Length: 13min 38sec (818 seconds)
Published: Thu Jan 28 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.