【发布时间】:2016-03-26 00:36:58
【问题描述】:
我试图使用 C# 脚本统一创建汽车,一切似乎都很好,汽车左右旋转,除了 1 个问题。当我按下前进或后退键时,汽车会左右移动而不是前进和后退,我看不出我的代码有什么问题,这是我的代码:
using UnityEngine;
using System.Collections;
public class Car : MonoBehaviour {
private Rigidbody carRigid;
public int speed;
public int rotateSpeed;
// Use this for initialization
void Start () {
carRigid = GetComponent<Rigidbody> ();
}
// Update is called once per frame
void Update () {
}
void FixedUpdate(){
if (Input.GetButton("Forward")){
carRigid.AddForce (transform.forward * speed * Time.deltaTime);
}
if (Input.GetButton("Reverse")){
carRigid.AddForce (transform.forward * -speed * Time.deltaTime);
}
if (Input.GetButton("Left")){
transform.Rotate (0, rotateSpeed, 0);
}
if (Input.GetButton("Right")){
transform.Rotate (0, rotateSpeed, 0);
}
}
}
【问题讨论】:
-
你的车在你不转弯的时候会前后走,但在你转弯后却是横着走?
-
不,它总是横着走。即使我从未按下旋转按钮。