国产探花免费观看_亚洲丰满少妇自慰呻吟_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);	}}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 玉林市| 昌江| 紫金县| 谢通门县| 巫山县| 克什克腾旗| 库车县| 绩溪县| 安宁市| 昌邑市| 渝北区| 苍溪县| 郯城县| 樟树市| 共和县| 肇庆市| 佛山市| 和林格尔县| 曲阜市| 晋中市| 钟山县| 新竹市| 固始县| 丰县| 云安县| 布拖县| 安西县| 襄垣县| 富顺县| 东兴市| 龙江县| 慈溪市| 鄯善县| 黔江区| 任丘市| 玛沁县| 疏勒县| 法库县| 敖汉旗| 伊金霍洛旗| 上饶市|