rigid body velocity in local space

I’m trying to make the velocity of a rigid body affected in local space instead of the global space.

        float moveX = InputH * speed * Time.deltaTime;
        float moveZ = InputV * speed * Time.deltaTime;
        rb.velocity = new Vector3(moveX, 0f, moveZ);

.
currently pressing a direction on the joystick runs the character is the same direction regardless of the camera angle, is there a way to make the characters velocity relate to the players current position? or camera angle?

and help really appreciated.
Thanks.

Yes! With Math!

I guess, think about how to add up the vector of Camera’s forward with the controller input to get what you want.

Before I get a pen and paper are you sure there’s an answer to be found?

many ways to do it, these are the two i recommend:

  1. multiply the vector with the players rotation, take the quaternion that is rotated on the euler Y and multiply it with the input. (I can give you a snippet when i get home)

  2. use this : Unity - Scripting API: Transform.InverseTransformDirection

Like @doctorpangloss said you could also use the transform .forward and .right

I’ve spent hours trying to understand this but i just cant wrap my head around the maths or the syntax can anyone give me another nudge in the right direction?

Finally cracked it, thanks for your suggestions.

1 Like