国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Unity-賽車Demo

2019-11-08 03:27:47
字體:
來源:轉載
供稿:網友
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);	}}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 太湖县| 威信县| 合水县| 苏尼特右旗| 丰县| 罗平县| 德保县| 尼勒克县| 靖江市| 乐安县| 岗巴县| 阿荣旗| 万年县| 朔州市| 海林市| 柏乡县| 醴陵市| 洛宁县| 门头沟区| 吐鲁番市| 麻江县| 台山市| 卓尼县| 孟村| 泰顺县| 嫩江县| 太原市| 临江市| 临沭县| 焦作市| 葫芦岛市| 寻甸| 阿克苏市| 淮滨县| 铁岭县| 营山县| 江达县| 呼伦贝尔市| 衡山县| 铜陵市| 呼伦贝尔市|