Rotate Vector3 by Direction

Hey folks!

I’m creating a custom Character Controller that deals with the normal of a Raycast. I cannot for the life of me figure out how best to rotate the resulting vector3 by the direction my player is looking. My current code is

Vector3 _dir = (Quaternion.AngleAxis(-90f, Vector3.right) * _hit.normal);

Where _hit is the hit of a raycast. I need to figure out how to rotate the Y axis of _dir so that the resulting vector is facing the same direction as my player.
Any help would be greatly appreciated!

Maybe something like this?

Player.transform.up = _dir.up;

you simply multiply the player’s quaternion by the target vector direction. for example:

// get the world direction the player is facing
Vector3 playerForward = playerTransform.rotation * Vector3.forward;

// get the world direction the fighter jet perceives as up
Vector3 jetUp = fighterJetTransform.rotation * Vector3.up;

now if its a simple cardinal direction you can use the helper properties on the transform to get them for you

// get the world direction the player is facing
Vector3 playerForward = playerTransform.forward;

// get the world direction the fighter jet perceives as up
Vector3 jetUp = fighterJetTransform.up;

but if you are looking to a specific offset from those cardinal directions you can apply those vector offset to the first example

1 Like