- Home /
Question by
mercsen · Apr 26, 2016 at 04:27 PM ·
velocityforcevector2calculations
calculate velocity vector based on direction
Hi folks,
I'm new to unity and currently I'm running some tests. One test is a power up a player can obtain. I want the player accelerated into the direction the powerup is faceing. Since it is a 2D game the direction is tied to the z-rotation. 0=up, 90=left, 180=down, 270=right. So if the power up is rotated at 90° i want a force applied to the player. This is easy for the base direction, 0,90,180 and 270. But how do i calculate the vector for e.g. 22° oder 98°?
I must admit my math knowledge is long gone and I'm currently working my way back to what i used to know. But until then i would appreciate a little hint :)
Thanks in advance, Marc
Comment
Best Answer
Answer by mercsen · Apr 26, 2016 at 06:51 PM
Ok i found the solution!
float angle = transform.eulerAngles.z * Mathf.Deg2Rad;
// caluclate the vector based on the rotation of the power up
// Vector.X = SINUS of angle in RADIANS
// Vector.Y COSINUS of angle in RADIANS
// 1 DEG = (1 * Mathf.Deg2Rad) RADIDANS
// somehow the x coordinates are flipped, so multiply by -1
Vector2 vect = new Vector2(Mathf.Sin(angle)*-1, Mathf.Cos (angle));
// add the force to the player
_playerRB.AddForce (vect * turbo);
its that simple :)