01unity虚拟摇杆制作,Unity 赛车控制器

脚本挂载在车子上车子需要具有Rigidbody2D组件

其中子物体:

wheelFront:车子前轮,有CircleCollider,WheelJoint2D和Rigidbody2D组件

wheelBack:车子后轮,有CircleCollider,WheelJoint2D和Rigidbody2D组件

wheelFrontTrigger车子前轮下方的检测器,需要勾选Trigger的CircleCollider和属性为Kinematic的Rigidbody2D组件,以及一个简单的GetWheelTrigger脚本(文章末尾)

wheelFrontTrigger车子后轮下方的检测器,需要勾选Trigger的CircleCollider和属性为Kinematic的Rigidbody2D组件,以及一个简单的GetWheelTrigger脚本(文章末尾)

using System;using System.Collections;using UnityEngine;public class CarControoler : MonoBehaviour{private WheelJoint2D wheelFront, wheelBack;private Rigidbody2D wheelFrontRig, wheelBackRig;private GetWheelTrigger wheelFrontTrigger, wheelBackTrigger;private Rigidbody2D myRig;private float speed = 0.0f;private float speedUp = 25.0f;private float brakeSpeed = 25.0f;private float speedMax = 2000.0f;private float holdPower = 1000.0f;private JointMotor2D frontMotor = new JointMotor2D();private JointMotor2D backMotor = new JointMotor2D();private void Start(){wheelFront = transform.Find("wheelFront").GetComponent();wheelBack = transform.Find("wheelBack").GetComponent();wheelFrontTrigger = transform.Find("wheelFrontTrigger").GetComponent();wheelBackTrigger = transform.Find("wheelBackTrigger").GetComponent();myRig = GetComponent();wheelFrontRig = wheelFront.GetComponent();wheelBackRig = wheelBack.GetComponent();}// Update is called once per framevoid Update(){if (!wheelFrontTrigger.isInTrigger && !wheelBackTrigger.isInTrigger){if (Input.GetKey(KeyCode.D)){transform.Rotate(0,0,-1);}if (Input.GetKey(KeyCode.A)){transform.Rotate(0,0,1);}}if (Input.GetKey(KeyCode.W)){speed += speedUp;SetWheelRig(speed > 0);}else if (Input.GetKey(KeyCode.S)){speed -= speedUp;SetWheelRig(speed < 0);}else{SetWheelRig(false);if (speed > brakeSpeed){speed -= brakeSpeed;}else if(speed < -brakeSpeed){speed += brakeSpeed;}else{speed = 0;}}}private void FixedUpdate(){speed = speed = -speedMax ? speed : -speedMax;frontMotor.maxMotorTorque = float.MaxValue;backMotor.maxMotorTorque = float.MaxValue;if (!wheelFrontTrigger.isInTrigger || !wheelBackTrigger.isInTrigger){wheelFront.useMotor = false;wheelBack.useMotor = false;if(!wheelFrontTrigger.isInTrigger){speed = wheelBack.jointSpeed;}if(!wheelBackTrigger.isInTrigger){speed = wheelFront.jointSpeed;}}else{frontMotor.motorSpeed = speed;backMotor.motorSpeed = speed;wheelFront.motor = frontMotor;wheelBack.motor = backMotor;}}private IEnumerator Wait(Action action){yield return new WaitForEndOfFrame();action();}public void AddForce(Vector2 force){myRig.AddForce(force * myRig.gravityScale);}void SetWheelRig(bool fast){if (fast){wheelFrontRig.angularDrag = 0;wheelBackRig.angularDrag = 0;}else{wheelFrontRig.angularDrag = holdPower;wheelBackRig.angularDrag = holdPower;}}}

其中涉及到的GetWheelTrigger代码为:

using System.Collections;using System.Collections.Generic;using UnityEngine;public class GetWheelTrigger : MonoBehaviour{public bool isInTrigger;private void OnTriggerEnter2D(Collider2D other){isInTrigger = true;}private void OnTriggerExit2D(Collider2D other){isInTrigger = false;}}
中级农业经济师报考条件天地英雄出了手游吗02512340是什么电话03348市场调查方法与技术安徽省供销集团有限公司安徽辉隆湖南境内自驾游景点排名手机谷歌浏览器怎么设置离久童30星名士高胜率上分阵容推荐白镜属性问题NGA玩家社区青木瓜长什么样子会计论文题目洪荒变成女凤凰穿越鸿蒙变身成女024白家夫人03冰山美人是谁069章砍瓜切菜0304改18禁CG尻肛辱怎么破解docx文件手机docx文件密码怎么解047车厢见面048车厢温情