好吧,我知道 Headers 并不是那么清楚......我有一个角色/僵尸,而且'她'可以转身 . 当我在Unity中按Play时,如果我向前/向后移动,那很好,她向前/向后移动 . 这是一个好的开始 .
但是如果我向左或向右转,然后前进/后退,她现在向侧面移动 .
她是一个刚体组件,设置为场景中的父级 .
当然这并不难做到,但我无法弄清楚如何设置她的旋转,以便当她转身时,当我按下按钮向前移动时,她将向前移动!有很多第一人称射击游戏,你可以转向并向前移动,玩家朝着正确的方向前进 .
我的旋转脚本目前是这样的:
Vector3 EulerAngleVelocity;
public float rotateSpeed = 250.0f;
void Update() {
if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.DownArrow))
{
MoveVector = PoolInput();
Move();
}
if (Input.GetKey(KeyCode.RightArrow))
{
EulerAngleVelocity = new Vector3(0, rotateSpeed, 0);
Quaternion deltaRotation = Quaternion.Euler(EulerAngleVelocity * Time.deltaTime);
rigidbody.MoveRotation(rigidbody.rotation * deltaRotation);
}
}
private void Move()
{
rigidbody.AddForce((MoveVector * moveSpeed));
}
private Vector3 PoolInput()
{
Vector3 dir = Vector3.zero;
dir.x = joystick.Horizontal();
dir.z = joystick.Vertical();
if (dir.magnitude > 1)
dir.Normalize();
return dir;
}
任何帮助将不胜感激,我无法想出这一点,但肯定很简单!
2 回答
你正在移动你的操纵杆并添加与WORLD相关的方向,而不是与你的玩家相关 . 如果你想相对于RigidBody的方向添加力可能你想要使用的是
rigidBody.AddRelativeForce
(documentation)而不是简单的rigidBody.AddForce
.你的问题不是轮换代码,而是你的动作代码 . 你在世界空间而不是局部空间('物体空间)中应用运动 .
例如,如果您使用
Vector3.Forward
,则需要使用transform.Forward
.