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!

Why do all that clever maths stuff when you can write transform.TransformVector and transform.LookAt :wink:

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.