控制汽车的速度

时间:2015-07-29 08:33:19

标签: c# unity3d

我一直试图在Unity4中控制汽车的速度。我在下面提供代码详细信息。我需要升级代码吗?因为当按下制动器(空格键)时,速度设置为零,但是当释放制动器时,速度再次增加。

using UnityEngine;
using System.Collections;

public class CarMovementScript : MonoBehaviour
{


    public Vector3 com;
    public Rigidbody rb;
    public WheelCollider FrontLeft;
    public WheelCollider FrontRight;
    public WheelCollider RearRight;
    public WheelCollider RearLeft;
    public float maxspeed = 40;
    public float carspeed = 0;
    public float speed = 0.0f;
    float braking = 75.0f;
    float turning = 30.0f;

    void Start()
    {

        rb = GetComponent<Rigidbody>();
        rb.centerOfMass = new Vector3(rb.centerOfMass.x, -0.9f, rb.centerOfMass.z);
    }
    void Update()
    {
        if (speed <= maxspeed)
        {
            Debug.Log(speed);
            //this code makes car go forward
            RearRight.motorTorque = Input.GetAxis("Vertical") * speed;
            RearLeft.motorTorque = Input.GetAxis("Vertical") * speed;
            speed += 0.05f;
        }



        //this code works for braking of the car
        RearRight.brakeTorque = 0;
        RearLeft.brakeTorque = 0;

        //this code is for turning
        FrontRight.steerAngle = Input.GetAxis("Horizontal") * turning;
        FrontLeft.steerAngle = Input.GetAxis("Horizontal") * turning;


        //Breaking
        if (Input.GetKey(KeyCode.Space))
        {
            RearRight.brakeTorque = braking;
            RearLeft.brakeTorque = braking;
            speed = 0.0f;

        }
    }
}

3 个答案:

答案 0 :(得分:0)

不要将速度设置为0而是将其减小0.05f。

删除此

if (speed <= maxspeed)
{
    Debug.Log(speed);
    //this code makes car go forward
    RearRight.motorTorque = Input.GetAxis("Vertical") * speed;
    RearLeft.motorTorque = Input.GetAxis("Vertical") * speed;
    speed += 0.05f;
}

并更改

if (Input.GetKey(KeyCode.Space))
{
    RearRight.brakeTorque = braking;
    RearLeft.brakeTorque = braking;
    speed = 0.0f;

}

if (Input.GetKey(KeyCode.Space))
{
    RearRight.brakeTorque = braking;
    RearLeft.brakeTorque = braking;
    speed -= 0.05f;

}
else if (speed <= maxspeed)
{
   Debug.Log(speed);
   //this code makes car go forward
   RearRight.motorTorque = Input.GetAxis("Vertical") * speed;
   RearLeft.motorTorque = Input.GetAxis("Vertical") * speed;
   speed += 0.05f;
}

答案 1 :(得分:0)

根据您的陈述:

when the brake is released the Speed is increasing again.

我的猜测是你的这段代码需要注意:

    if (speed <= maxspeed)
    {
        Debug.Log(speed);
        //this code makes car go forward
        RearRight.motorTorque = Input.GetAxis("Vertical") * speed;
        RearLeft.motorTorque = Input.GetAxis("Vertical") * speed;
        speed += 0.05f;
    }

当你应用休息时,速度会变为零,但是从speed <= maxspeed开始,所以它返回true并且由于speed += 0.05f而开始移动汽车。所以除非有垂直轴的输入,否则你应该阻止这种情况。喜欢:(未经测试,给你一个想法)

    float vertical = Input.GetAxis("Vertical");
    if ((speed != 0 && speed <= maxspeed) || 
        (speed == 0.0 && vertical > 0.0))
    {
        Debug.Log(speed);
        //this code makes car go forward
        RearRight.motorTorque = vertical * speed;
        RearLeft.motorTorque = vertical * speed;
        speed += 0.05f;
    }

这样,如果条件将执行,如果制动器应用并且从垂直轴有速度增益已经有速度增益,速度受限于maxspeed

希望它有所帮助!

答案 2 :(得分:0)

这是转弯的代码。但转折在开始时自动成倍增加。必须进行哪些代码更改才能调整转弯? FrontRight.steerAngle = Input.GetAxis(“水平”)*转动;         FrontLeft.steerAngle = Input.GetAxis(“水平”)*转动;