Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
526 views
in Technique[技术] by (71.8m points)

c# - Unity3d Local Forward

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

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

1 Reply

0 votes
by (71.8m points)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...