Unity汽车控制器故障:车轮异常旋转脱落,求最优实现方案
关于Unity汽车控制器实现的问题与优化判断
初始问题
- 运行模式下前轮沿Z轴旋转90度


- 车轮直接从车上脱落


初始汽车层级结构

初始实现代码
using System.Collections; using System.Collections.Generic; using UnityEngine; public class playerMovement : MonoBehaviour { [SerializeField] WheelCollider leftFront; [SerializeField] WheelCollider rightFront; public Transform leftFrontWheelTransform; public Transform rightFrontWheelTransform; public GameObject player; public Transform toBeRolled; private float maxTurnAngle = 30f; private float currentTurnAngle = 30f; public static float currentSpeed; public static float DriveSpeed = 0.0f; public static float slowSpeed; public float steerAmount = 120f; private float steer; private float timer = 0f; private float SecondCount = 1f; //Delay time void Update() { slowSpeed = DriveSpeed * 0.7f; //slowSpeed = 5(initial) * 0.7 => 3.5 initially. currentSpeed = DriveSpeed; //5 if(Input.GetKey(KeyCode.Space)) { currentSpeed = slowSpeed; } timer += Time.deltaTime; player.transform.position += player.transform.forward * (currentSpeed) * Time.deltaTime; float _horizontal = Input.GetAxis("Horizontal"); steer += _horizontal * steerAmount * Time.deltaTime; float roll = Mathf.Lerp(0, 0, Mathf.Abs(_horizontal)) * -Mathf.Sign(_horizontal); float rollSTEAR = Mathf.Lerp(0, 10, Mathf.Abs(_horizontal)) * -Mathf.Sign(_horizontal); transform.localRotation = Quaternion.Euler(Vector3.up * steer + Vector3.forward * roll); toBeRolled.localRotation = Quaternion.Euler(Vector3.forward * rollSTEAR); currentTurnAngle = maxTurnAngle * _horizontal; leftFront.steerAngle = currentTurnAngle; rightFront.steerAngle = currentTurnAngle; UpdateWheel(leftFront , leftFrontWheelTransform); UpdateWheel(rightFront , rightFrontWheelTransform); if(timer > SecondCount) { DriveSpeed += 0.00001f; //speed up text & sound on screen timer = 0f; } } void UpdateWheel(WheelCollider col, Transform trans) { Vector3 position; Quaternion rotation; col.GetWorldPose(out position, out rotation); trans.position = position; trans.rotation = rotation; } }
更新后的实现
经过调整层级结构与代码,当前功能正常,但不确定是否为最优方案:
修改后的汽车层级结构

更新后的代码
using JetBrains.Annotations; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Rendering.Universal; public class CarMovement : MonoBehaviour { public WheelCollider leftFront; public WheelCollider rightFront; public WheelCollider leftRear; public WheelCollider rightRear; public Rigidbody car; public Transform LF_wheel; public Transform RF_wheel; public Transform LR_wheel; public Transform RR_wheel; public float maxTorq; public float maxSteerAngle; void Start() { car = GetComponent<Rigidbody>(); } void Update() { takeingInput(); steer(); updateWheels(); } public void takeingInput() { float _vertical = Input.GetAxis("Vertical"); //leftRear.motorTorque = _vertical * maxTorq; //rightRear.motorTorque = _vertical * maxTorq; leftRear.motorTorque = Mathf.Clamp(_vertical, -1.0f, 1.0f) * maxTorq; rightRear.motorTorque = Mathf.Clamp(_vertical, -1.0f, 1.0f) * maxTorq; } public void steer() { float _horizontal = Input.GetAxis("Horizontal"); leftFront.steerAngle = Mathf.Clamp(_horizontal, -1.0f, 1.0f) * maxSteerAngle; rightFront.steerAngle = Mathf.Clamp(_horizontal, -1.0f, 1.0f) * maxSteerAngle; LF_wheel.localRotation = Quaternion.Euler(0,leftFront.steerAngle,90); RF_wheel.localRotation = Quaternion.Euler(0,rightFront.steerAngle,90); } public void updateWheels() { wheelUpdate(leftFront, LF_wheel); wheelUpdate(rightFront, RF_wheel); wheelUpdate(leftRear, LR_wheel); wheelUpdate(rightRear, RR_wheel); wheelUpdateFix(LR_wheel); wheelUpdateFix(RR_wheel); } public void wheelUpdate(WheelCollider col, Transform trans) { Vector3 pos; Quaternion rot; col.GetWorldPose(out pos, out rot); trans.position = pos; //trans.rotation = rot; } public void wheelUpdateFix(Transform trans) { Quaternion _fixeRot = transform.rotation; _fixeRot = _fixeRot * Quaternion.Euler(0, 0, -90); trans.rotation = _fixeRot; } }
问题请求
判断当前更新后的实现是否为最优方案,还是属于暴力修复?
内容的提问来源于stack exchange,提问作者Faizal
相关产品推荐
相关产品推荐

