0
\$\begingroup\$

in my game the player is to walk around a sphere, I'm using rigidbody to apply movement with a joystick like this;

rb.velocity = new Vector3(move.x, move.y)* speed;

The problem is that when a character walks around a sphere, they rotate, so the movement gets messed up, how do I make them move relative to the current rotation and not the world?

I'm new to unity and coding in general so showing example code would help a lot!

\$\endgroup\$
0
\$\begingroup\$

This can be a one-liner:

rb.veloxity = transform.rotation * move * speed;

Multiplying a Quaternion rotation by a vector rotates the vector by that rotation.

Unity will also automatically promote a Vector2 to a Vector3 (or back), so we don't need to make a new Vector3() explicitly here.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.