Unity-赛车Demo

本文介绍了一个使用Unity和C#实现的汽车驾驶模拟器。该模拟器通过四个轮子的独立控制来模拟汽车的行驶状态,包括加速、转向和制动等功能,并实现了换挡逻辑和危险检测机制。
using UnityEngine;
using System.Collections;

public class CarDrive : MonoBehaviour {

    public WheelCollider wheelColLF;
    public WheelCollider wheelColLB;
    public WheelCollider wheelColRF;
    public WheelCollider wheelColRB;

    public Transform transWheelLF;
    public Transform transWheelLB;
    public Transform transWheelRF;
    public Transform transWheelRB;

    public float motorTorqueMax = 600;
    float motorCurrentTorque = 0;//当前扭矩
    public float steerAngleMax = 25;
    public float brakeTorqueMax = 3000;
    Rigidbody rigid;

    //发动机最大转速
    public float maxEngineRPM = 1500;
    //发动机最小转速
    public float minEngineRPM = 200;
    //档位(车轮半径/发动机齿轮半径)
    public float[] gearRatios; 
    //当前档位编号
    int gearIndex = 0;

    float dangousTime=0;
    Vector3 safePosition;
    Quaternion safeRotation;

    void Start()
    {
        rigid = GetComponent<Rigidbody>();
        Vector3 center = rigid.centerOfMass;//车的重心
        center.y = 0.2f;
        rigid.centerOfMass = center;
        FinalPoint.instance.finalPointEvent += OnFinal;
        CarNumber.instance.startRaceEvent += OnStart;
    }
	public void Drive (float v,float h,float b)
    {
        GearControl();
        motorCurrentTorque = motorTorqueMax * gearRatios[gearIndex];
        //前进
        wheelColLF.motorTorque = motorCurrentTorque * v;
        wheelColRF.motorTorque = motorCurrentTorque * v;

        //转弯
        wheelColLF.steerAngle = steerAngleMax * h;
        wheelColRF.steerAngle = steerAngleMax * h;

        //刹车
        wheelColLF.brakeTorque = brakeTorqueMax * b;
        wheelColLB.brakeTorque = brakeTorqueMax * b;
        wheelColRF.brakeTorque = brakeTorqueMax * b;
        wheelColRB.brakeTorque = brakeTorqueMax * b;

        SetWheelPose(transWheelLF, wheelColLF);
        SetWheelPose(transWheelLB, wheelColLB);
        SetWheelPose(transWheelRF, wheelColRF);
        SetWheelPose(transWheelRB, wheelColRB);

        DangousCheck();
	}

    void SetWheelPose(Transform transWheel,WheelCollider wheelCollider)
    {
        Vector3 pos;
        Quaternion quat;
        wheelCollider.GetWorldPose(out pos, out quat);
        transWheel.position = pos;
        transWheel.rotation = quat;
    }

    void GearControl()
    {
        if (wheelColLF.rpm*gearRatios[gearIndex]>maxEngineRPM)
        {
             gearIndex++;
             gearIndex = Mathf.Clamp(gearIndex, 0, gearRatios.Length - 1);
        }
        if (wheelColLF.rpm*gearRatios[gearIndex]<minEngineRPM)
        {
              gearIndex--;
              gearIndex = Mathf.Clamp(gearIndex, 0, gearRatios.Length - 1);
        } 
    }

    void DangousCheck()
    {
        int countWheelOnGround = 0;
        if(wheelColLF.isGrounded)
            countWheelOnGround++;
        if(wheelColLB.isGrounded)
            countWheelOnGround++;
        if(wheelColRF.isGrounded)
            countWheelOnGround++;
        if(wheelColRB.isGrounded)
            countWheelOnGround++;

        if(countWheelOnGround==4)
        {
            safePosition = transform.position;
            safeRotation = transform.rotation;
        }    
        if (countWheelOnGround <= 2)
            dangousTime += Time.deltaTime;
        else
            dangousTime = 0;
        if (dangousTime>5)
        {
            transform.position = safePosition + Vector3.up * 0.5f;
            transform.rotation = safeRotation;
            dangousTime = 0;
        }
    }
}


using UnityEngine;
using System.Collections;

public class UserInput : MonoBehaviour {

    CarDrive carDrive;
	void Start () {
        carDrive = GetComponent<CarDrive>();
	}

    float accelerator = 0;//油门
    float accelAddSpeed = 0.2f;
	void Update () {
        float v = Input.GetAxis("Vertical");
        float h = Input.GetAxis("Horizontal");
        float b = Input.GetAxis("Jump");//刹车
        if (Mathf.Abs(v)>0.5f)
        {
            accelerator += v * accelAddSpeed*Time.deltaTime;
        }
        else
        {
            accelerator = 0;
        }
        accelerator = Mathf.Clamp(accelerator, -0.2f, 1);
        carDrive.Drive(accelerator, h, b);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值