Delay in control and involuntary clamping of Wheels Collider

1 day ago 3
ARTICLE AD BOX

I was making a small scene for a project at the institute. I tried to implement the gears, but I couldn't. I rolled everything back as much as I could, but now the car is behaving strangely. If you press the gas, it will accelerate even if you've already released it. There's also a noticeable delay before braking or starting.

The most interesting thing is that the car drives automatically by holding down the gas button for about as long as I hold down the button.

public class Car : MonoBehaviour { [SerializeField] private WheelCollider[] _frontWheels; [SerializeField] private WheelCollider[] _rearWheels; [SerializeField] private TextMeshProUGUI textt; [SerializeField] private float _maxMotorFroce = 1000; [SerializeField] private float _maxSteeringAngle = 30; [SerializeField] private float _brakeForce; [SerializeField] private float _RRbrakeForce; [SerializeField] private float silaTormoz; [SerializeField] private float silaRuchnik; [SerializeField] private float tormozeniya; [SerializeField] private Rigidbody rg; [SerializeField] private GameObject[] points; private float motorInput; private float steeringInput; private float brakeInput; private void Start() { rg = gameObject.GetComponent<Rigidbody>(); } private void Update() { float bb = rg.velocity.magnitude * 3.6f; textt.text = bb.ToString(); steeringInput = Input.GetAxis("Horizontal") * _maxSteeringAngle; Vector3 glawa = points[0].transform.position + rg.velocity; Vector3 biba = points[1].transform.position - glawa; Vector3 boba = points[2].transform.position - glawa; if (biba.magnitude < boba.magnitude) { motorInput = Input.GetKey(KeyCode.W) ? _maxMotorFroce : 0; brakeInput = Input.GetKey(KeyCode.S) ? _brakeForce : 0; } else if (biba.magnitude > boba.magnitude) { motorInput = Input.GetKey(KeyCode.S) ? _maxMotorFroce * -1 : 0; brakeInput = Input.GetKey(KeyCode.W) ? _brakeForce : 0; } else { motorInput = Input.GetKey(KeyCode.W) ? _maxMotorFroce : 0; motorInput = Input.GetKey(KeyCode.S) ? _maxMotorFroce * -1 : 0; } if (Input.GetKeyDown(KeyCode.Space)) { for (int i = 0; i < _rearWheels.Length; i++) { WheelFrictionCurve sidewaysCurve = _rearWheels[i].sidewaysFriction; WheelFrictionCurve sggge = _rearWheels[i].forwardFriction; sidewaysCurve.stiffness /= 2; sggge.stiffness /= 2; _rearWheels[i].forwardFriction = sggge; _rearWheels[i].sidewaysFriction = sidewaysCurve; Debug.Log("Пробел нажат!"); } } if (Input.GetKeyUp(KeyCode.Space)) { for (int i = 0; i < _rearWheels.Length; i++) { WheelFrictionCurve sidewaysCurve = _rearWheels[i].sidewaysFriction; WheelFrictionCurve sggge = _rearWheels[i].forwardFriction; sidewaysCurve.stiffness *= 2; sggge.stiffness *= 2; _rearWheels[i].forwardFriction = sggge; _rearWheels[i].sidewaysFriction = sidewaysCurve; Debug.Log("Пробел отпущен!"); } } } private void FixedUpdate() { steeringInput = Input.GetAxis("Horizontal") * _maxSteeringAngle; for (int i = 0; i < _frontWheels.Length; i++) { _frontWheels[i].steerAngle = steeringInput; _frontWheels[i].brakeTorque = brakeInput; UpdateWheelPose(_frontWheels[i]); } for (int i = 0; i < _rearWheels.Length; i++) { _rearWheels[i].motorTorque = motorInput; _rearWheels[i].brakeTorque = brakeInput; if (motorInput == 0 && brakeInput == 0) { _rearWheels[i].brakeTorque = 200f; } UpdateWheelPose(_rearWheels[i]); } } private void UpdateWheelPose(WheelCollider wheel) { if (wheel.transform.childCount == 0) return; Transform child = wheel.transform.GetChild(0); wheel.GetWorldPose(out Vector3 position, out Quaternion rotation); child.position = position; child.rotation = rotation; } }
Read Entire Article