【发布时间】:2017-09-07 09:32:07
【问题描述】:
我是 Unity 和刚体的新手,我想通过尝试制作 3D Tron Light-cycle 游戏来学习。我使用圆柱体、球体和矩形的组合制作了我的玩家载具,如下所示:
我在细长的球体上使用了刚体,并使用了以下代码:
public float accel = 1.0f;
// Use this for initialization
void Start () {
cycleSphere = GetComponent<Rigidbody>();
}
void FixedUpdate () {
cycleSphere.velocity = Vector3.forward * accel;
}
这会使车辆向前移动。我不确定是否有更好的方法来做到这一点,但如果有,请务必说出来。
我已将主摄像头连接到车辆,并禁用 X 旋转以防止它和摄像头滚动。
现在我想通过按 A 和 D 按钮让它转动。与原始 Tron 光循环的 90 度转向不同,我希望它像普通车辆一样转向。
所以我尝试了这个:
void Update () {
if (Input.GetKey (KeyCode.A)) {
turning = true;
turnAnglePerFixedUpdate -= turnRateAngle;
} else if (Input.GetKey (KeyCode.D)) {
turning = true;
turnAnglePerFixedUpdate += turnRateAngle;
} else {
turning = false;
}
}
void FixedUpdate () {
float mag = cycleSphere.velocity.magnitude;
if (!turning) {
Quaternion quat = Quaternion.AngleAxis (turnAnglePerFixedUpdate, transform.up);// * transform.rotation;
cycleSphere.MoveRotation (quat);
}
cycleSphere.velocity = Vector3.forward * accel;
}
虽然上面的代码确实旋转了载具,但它仍会沿其上一个方向移动 - 它的行为更像是坦克炮塔。更糟糕的是,过多地按下 A 或 D 会导致它向所需方向旋转,然后过一会就会发疯,左右旋转,带着相机。
我做错了什么,我该如何解决?
【问题讨论】:
标签: c# unity3d rigid-bodies