This site uses strictly necessary cookies. More Information

X- Home /

# 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

**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 :)

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

Increasing the speed of a ball in a brick breaker game 0 Answers

Clamping velocity and angular velocity at the same time? 0 Answers

Rigidbody velocity has got different direction than VelocityChange force vector 1 Answer

How to not get velocity by the other objects?,How to not get force by other gameobjects? 0 Answers