Up Velocity
I watched this tutorial: https://www.youtube.com/watch?v=0Quv9U9_a8c about making a racing game in unity, but I didn't understand why the ForwardVelocity() function was returning only the up velocity.
From 32:40 he starts explaining what he wants to do, and how.
Answer by Major · Jul 09, 2017 at 05:00 PM
The two functions, ForwardVelocity () and RightVelocity (), return the individual components of the car's velocity vector. He does this so that he can more easily manipulate them individually.
Why did he use the dot product? Couldn't he just do this :
rb.velocity = new vector2(rb.velocity.x*0.9,rb.velocity.y);
Because he wanted to kill 10% of the right velocity.
@andreighinea1 I am so sorry about the delayed reply, I didn't get an email... Damn forums...
What quill was doing was returning the world space conversion of the local forward and right vectors. In essence the equivalent of doing:
Vector3 forwardVec = transform.TransformVector (transform.forward);
and
Vector3 rightVec = transform.TransformVector (transform.right);
While his method of using dot is valid, as you can see it's more complicated and he didn't explain it.. well, at all.
Your method is exactly the same, given you convert the local forward, and right vectors to world space before you did your operations.
Again, sorry about the lateness of this, I hope that helps you understand his code a little better.
@$$anonymous$$ajor
Thanks! Now I understand why he had to do the dot product of the velocity of the player and the world up axis ... Just to convert the local player axis to the world axis.
But when I try to do like you said it doesn't really work:
{...
rb.velocity = UpVelocity () + RightVelocity () * 0.9f;
}
Vector2 UpVelocity(){
return transform.TransformVector (transform.up);
}
Vector2 RightVelocity(){
return transform.TransformVector (transform.right);
}
The x velocity of the car is always around 0.9 and the y velocity is around 1. That's because the transform.up is in world space and it's always 1, same for the right one.
Your answer
Follow this Question
Related Questions
Choosing speed and direction of AddForce 1 Answer
Moving GameObject a specific distance in the Z direction and back again - regardless of rotation 1 Answer
Unity network transform massive movement jitter jumps 0 Answers
Have object fly towards camera 0 Answers
Movement in the direction the user is facing with arrow keys 0 Answers