- 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>
/// $$anonymous$$agical 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 = $$anonymous$$athf.Sin(Angle);
float cosAngle = $$anonymous$$athf.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 $$anonymous$$d 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, 2021 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.
I apologize for not answering your comment. I don't really remember what happened but I just missed it.
Thank you very much!
I've since solved the issue (not really sure how that went). Thanks again :)