本文介绍了Unity3d本地转发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使刚体在局部坐标中向前移动,我的意思是,如果我旋转它,我希望它在其局部X轴上运动.
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.
推荐答案
MovePosition在世界空间中有效,因此您必须执行以下操作:
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);
这篇关于Unity3d本地转发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!