Help - Get Vector 3 To Match Player Object Forward

I’m playing around with the built-in character controller.

Unity - Scripting API: CharacterController.Move (unity3d.com)

in the jump code we have this,

playerVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);

Which got me to thinking, what if we made some changes to control jump distance

playerVelocity.z += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);

Works, except, it only moves forward in the world space no matter which direction the player object is facing.

So, what I’m trying to figure out is how do I get this Vector3 to match up with the direction the player is facing?

It’s late. I’ve hit a dead end here.

playerVelocity is a vector in worldspace, so .z is always world +Z

Instead you want to add a velocity vector based upon the player’s local +Z vector.

Handily enough there is a shortcut called transform.forward

playerVelocity += transform.forward * HowMuchForwardValue;

Same goes for up… if your player was tilted sideways, he could still jump up in his local frame with:

playerVelocity += transform.up * HowMuchUpValue;
3 Likes

Thanks. I was missing the " + " when I was messing around with this last night.