Unity车辆控制:如何实现未加速时自动减速?
车辆不加速时自动减速的实现方案
问题
希望车辆在不加速时自动减速,现有Unity车辆控制代码如下:
using UnityEngine; public class CarWheelControls : MonoBehaviour { [SerializeField] WheelCollider frontRight; [SerializeField] WheelCollider frontLeft; [SerializeField] WheelCollider BackRight; [SerializeField] WheelCollider BackLeft; [SerializeField] Transform frontRightTransform; [SerializeField] Transform frontLeftTransform; [SerializeField] Transform backRightTransform; [SerializeField] Transform backLeftTransform; public float accerelation = 500f; public float breakingForce = 300f; public float maxTurnAngle = 15f; private float currentAccerelation = 0f; private float currentBreakForce = 0f; private float currentTurnAngle = 0f; private void FixedUpdate(){ //Forward/Reverse acceleration currentAccerelation = accerelation * Input.GetAxis("Vertical"); //pressing "space" gives currentBreakingForce a value if (Input.GetKey(KeyCode.Space)) currentBreakForce = breakingForce; else currentBreakForce = 0f; // Apply accerelation on all wheels. Change to front wheel or rear // wheel drive by deleting fronts or rears :) frontRight.motorTorque = currentAccerelation; frontLeft.motorTorque = currentAccerelation; BackRight.motorTorque = currentAccerelation; BackLeft.motorTorque = currentAccerelation; //Wheel stopping frontRight.brakeTorque = currentBreakForce; frontRight.brakeTorque = currentBreakForce; BackRight.brakeTorque = currentBreakForce; BackLeft.brakeTorque = currentBreakForce; //Steering currentTurnAngle = maxTurnAngle * Input.GetAxis("Horizontal"); frontLeft.steerAngle = currentTurnAngle; frontRight.steerAngle = currentTurnAngle; //Wheel turning UpdateWheel(frontLeft, frontLeftTransform); UpdateWheel(frontRight, frontRightTransform); UpdateWheel(BackLeft, backLeftTransform); UpdateWheel(BackRight, backRightTransform); } void UpdateWheel(WheelCollider col, Transform trans){ //Wheel collider state Vector3 position; Quaternion rotation; col.GetWorldPose(out position, out rotation); //Set wheel angle trans.position = position; trans.rotation = rotation; } }
解决方案
要实现不加速时自动减速,只需修改刹车力的逻辑:当没有加速输入(即Vertical轴输入为0)时,给车轮施加一个较小的制动力;同时保留原有的空格急刹功能。具体步骤如下:
- 添加一个公共变量,用于控制自动减速的力度:
public float slowDownForce = 100f; // 可在Inspector中调整合适的数值
- 修改
FixedUpdate中的刹车力判断逻辑,替换原有的空格判断代码:
// 自动减速+急刹逻辑 float verticalInput = Input.GetAxis("Vertical"); if (Input.GetKey(KeyCode.Space)) { currentBreakForce = breakingForce; } else if (Mathf.Abs(verticalInput) < 0.01f) // 无加速输入时自动减速 { currentBreakForce = slowDownForce; } else { currentBreakForce = 0f; }
- 修复原代码中的笔误:原代码中
frontRight.brakeTorque重复赋值,漏掉了frontLeft的设置,应改为:
//Wheel stopping frontRight.brakeTorque = currentBreakForce; frontLeft.brakeTorque = currentBreakForce; // 补上这一行 BackRight.brakeTorque = currentBreakForce; BackLeft.brakeTorque = currentBreakForce;
修改后的完整代码
using UnityEngine; public class CarWheelControls : MonoBehaviour { [SerializeField] WheelCollider frontRight; [SerializeField] WheelCollider frontLeft; [SerializeField] WheelCollider BackRight; [SerializeField] WheelCollider BackLeft; [SerializeField] Transform frontRightTransform; [SerializeField] Transform frontLeftTransform; [SerializeField] Transform backRightTransform; [SerializeField] Transform backLeftTransform; public float accerelation = 500f; public float breakingForce = 300f; public float maxTurnAngle = 15f; public float slowDownForce = 100f; // 新增:自动减速力度 private float currentAccerelation = 0f; private float currentBreakForce = 0f; private float currentTurnAngle = 0f; private void FixedUpdate(){ float verticalInput = Input.GetAxis("Vertical"); //Forward/Reverse acceleration currentAccerelation = accerelation * verticalInput; // 自动减速+急刹逻辑 if (Input.GetKey(KeyCode.Space)) { currentBreakForce = breakingForce; } else if (Mathf.Abs(verticalInput) < 0.01f) // 无加速输入时自动减速 { currentBreakForce = slowDownForce; } else { currentBreakForce = 0f; } // Apply accerelation on all wheels. Change to front wheel or rear // wheel drive by deleting fronts or rears :) frontRight.motorTorque = currentAccerelation; frontLeft.motorTorque = currentAccerelation; BackRight.motorTorque = currentAccerelation; BackLeft.motorTorque = currentAccerelation; //Wheel stopping frontRight.brakeTorque = currentBreakForce; frontLeft.brakeTorque = currentBreakForce; // 修复笔误 BackRight.brakeTorque = currentBreakForce; BackLeft.brakeTorque = currentBreakForce; //Steering currentTurnAngle = maxTurnAngle * Input.GetAxis("Horizontal"); frontLeft.steerAngle = currentTurnAngle; frontRight.steerAngle = currentTurnAngle; //Wheel turning UpdateWheel(frontLeft, frontLeftTransform); UpdateWheel(frontRight, frontRightTransform); UpdateWheel(BackLeft, backLeftTransform); UpdateWheel(BackRight, backRightTransform); } void UpdateWheel(WheelCollider col, Transform trans){ //Wheel collider state Vector3 position; Quaternion rotation; col.GetWorldPose(out position, out rotation); //Set wheel angle trans.position = position; trans.rotation = rotation; } }
说明
slowDownForce的数值可以在Unity Inspector面板中调整,根据车辆重量和手感设置合适的力度,数值越大减速越快。- 使用
Mathf.Abs(verticalInput) < 0.01f判断是为了避免输入轴的微小波动导致误触发自动减速。
内容的提问来源于stack exchange,提问作者Ju_hb
相关产品推荐
相关产品推荐

