Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to move a Rigidbody forward in local coordinates, I mean, if I rotate it I want it to move in his local X axis.

I have tried this, but it moves in global coordinates:

Rigidbody player = GetComponent<Rigidbody>();

Vector3 movement = new Vector3 (1.0f, 0.0f, 0.0f);
movement = movement.normalized * 2 * Time.deltaTime;
player.MovePosition(transform.position + movement);

I don't know how to make the change to local coordinates.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
500 views
Welcome To Ask or Share your Answers For Others

1 Answer

MovePosition works in world space, so you have to do this:

Rigidbody player = GetComponent<Rigidbody>(); // I really hope you're not doing this every frame, btw
float speed = 2f; // magic numbers are bad, move them to variables, at least
Vector3 movement = transform.forward * speed * Time.deltaTime;
player.MovePosition(transform.position + movement);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...