- Home /

# Rotating a vector around a point

TL;DR How to rotate a Vector3 around an axis that is not parallel to the X, Y or Z axes?

Hi! First, I know there are a ton of question similar to this one, but I could not find what I was looking for.

I'm trying to create a 3D-ish top down game. The floor is rotated 40 degrees on the X-axis. I am trying to create an aiming system, where the player can either aim with the mouse or with an analog stick.

when I tried implementing the aiming in 2D and the surface was parallel to the Y-axis, It was just finding the direction vector between the player position and the mouse in world coordinates. but now the ground is tilted and its in 3D.

first I found a vector parallel to the tilted surface that will act as the new forward vector:

```
aimVector = Vector3.forward;
float groundRotation = -50f;
Quaternion angle = Quaternion.Euler(groundRotation, 0, 0);
aimVector = angle * aimVector;
```

I tried to find all 4 (forward, back, right, left) so I can try to interpolate between them, but that did not succeed.

Eventually, I want to be able to "project" the mouse position on the surface and have the player facing there.

Any help would be very appreciated, thank you!

You can rotate a Vector2 or coordinate, around a pivot point, by theta degrees, using some simple trig methods. I've written a method for this in C#, with the help of some geniuses on stack exchange below:

```
/// <summary>
/// Magical math function that helps to adjust fire direction
/// when camera is turning
/// </summary>
/// <param name="Point">Point to rotate</param>
/// <param name="Pivot">Pivot point to rotate point around</param>
/// <param name="Angle">By how many radians to rotate point by</param>
/// <returns>Rotated point positions</returns>
Vector2 RotatePointAroundPivot(Vector2 Point, Vector2 Pivot, float Angle)
{
float sinAngle = Mathf.Sin(Angle);
float cosAngle = Mathf.Cos(Angle);
Vector2 OriginPoint = new Vector2
(
Pivot.x - Point.x,
Pivot.y - Point.y
);
float NewXPoint = OriginPoint.x * cosAngle - OriginPoint.y * sinAngle;
float NewYPoint = OriginPoint.x * sinAngle + OriginPoint.y * cosAngle;
Vector2 RotatedPoint = new Vector2
(
NewXPoint + Point.x,
NewYPoint + Point.y
);
return RotatedPoint;
}
```

I actually don't think this is the answer to your question in mind when reading the description, but nevertheless, it is here.

I'm guessing you could rotate the mouse position by a pivot point and then re-add the z component? @unity_cJpDgSl63jDPGA

**Answer** by jackmw94
·
Jan 12 at 11:06 AM

Why do all that clever maths stuff when you can write `transform.TransformVector`

and `transform.LookAt`

;)

The former will change a vector from being in world space to as if it was a local vector under the transform on which it was called. i.e. if you have a transform that is exactly upside down then calling. `transform.TransformVector(Vector3.up)`

the result will be equal to Vector3.down as this is what up is in the local space of the upside down transform.

So if you have your floor transform you can do:

```
Vector3 lookVector = new Vector3(1f, 0f, 1f);
lookVector = floorTransform.TransformVector(lookVector);
```

To get a vector offset to the top right of the screen in "floor space".

Then instead of messing around with getting this offset vector into euler angles, use that as the target of transform.LookAt on the player to make the player's transform look at that target.

### 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

Flip over an object (smooth transition) 3 Answers

Multiple Cars not working 1 Answer

slowly rotate a object *need quick fix* 0 Answers