- Home /
The question is answered, right answer was accepted
need explanation for a line of code.
hi, i need explanation for a line of code.
public Vector2 speed = new Vector2(50, 50);
vector2 movement = new Vector2(speed.x * inputX,speed.y * inputY);
i know what it does. but i couldn't able to understand the concept here
**vector2 movement = new Vector2(speed.x * inputX,speed.y * inputY);**
need help in expaning.
Answer by CHPedersen · Jul 09, 2014 at 08:36 AM
First of all, correct the code. It's wrong, and won't compile. It should be:
public Vector2 speed = new Vector2(50, 50);
Vector2 movement = new Vector2(speed.x * inputX,speed.y * inputY);
Notice the capital "V", there.
Second of all, while studying the code, try to simply say out loud (to yourself!) what the code does, line by line, dot by dot, comma by comma. This is known as rubber duck debugging, and often helps understanding because saying things out loud forces your mind to process what the code does.
Here's an example, based on your code:
"Okay, line 1: Declare a Vector2 called 'speed'. It's two-dimensional, and it's a field variable, since it has an access modifier, which is set to public. Initialize this variable, 'speed', to hold a Vector2 whose values are set to x = 50, and y = 50. Okay, so speed is a two-dimensional vector holding 50, 50. Cool."
"Okay, line 2: Declare a new Vector2, called 'movement'. This one is a local variable, since it does not have an access modifier, and because it references 'speed' later on. Initialize 'movement' to a new Vector2 holding values for x and y which are multiplications of the x and y of 'speed', and of the inputX and inputY variables. Okay, so, inputX and inputY are probably read by input from the user, and their values will change based on what the user does. But the values of speed are constants - I know that, because I just set both of them to 50. Ah! Okay, so movement is just a vector that defines the direction of movement as per whatever the user does, and then the magnitude of the vector is scaled up using the speed numbers!".
End of story. :)
For clarity and readability, I also suggest always putting a space after a comma :)
Answer by jamesflowerdew · Jul 09, 2014 at 08:33 AM
input on the x and y will be between 1 and -1 depending on the player's actions (0 if they are not touching anything ;)). mutiplying these by set values will convert these actions into movement at in this case the max speed of 50.
this is kind-of crude, and does not create acceleration decelleration. If I wantes smooth play, I'd think of : public int speed=50;//they are same, so why bother with vector2 private Vector2 movement=new Vector2(0,0); movement+=new Vector2(speed inputX,speed inputY)Time.deltaTime; //reach optimal speed in roughly a second movement=(1-Time.deltaTime); //slow down in roughly a second.
That's mostly correct, but you don't know what values inputX and inputY might have based on his code. They could be declared like this:
float inputX = Input.GetAxis("$$anonymous$$ouse X");
The mouse can return values greater than 1 and less than -1.