I have a car controller for my 3D game, and it works well for the most part, but I have a slight problem with the movement. When I press “S” to move my car backwards after it is going forward, it takes a long time for the car to slow down and start moving backwards. This is also true vice versa, when I try to press “W” to move my car forwards after it is going backwards. I want to fix this, as this issue gives my car controller an unresponsive and slippery/sluggish feeling.
Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CarController : MonoBehaviour
{
private const string horizontal = "Horizontal";
private const string vertical = "Vertical";
private float horizontalInput;
private float verticalInput;
private float steerAngle;
private bool isBreaking;
[SerializeField] private float motorForce;
[SerializeField] private float brakeForce = 10000;
[SerializeField] private float maxSteerAngle;
[SerializeField] private WheelCollider frontLeftWheelCollider;
[SerializeField] private WheelCollider frontRightWheelCollider;
[SerializeField] private WheelCollider rearLeftWheelCollider;
[SerializeField] private WheelCollider rearRightWheelCollider;
[SerializeField] private Transform frontLeftWheelTransform;
[SerializeField] private Transform frontRightWheelTransform;
[SerializeField] private Transform rearLeftWheelTransform;
[SerializeField] private Transform rearRightWheelTransform;
[SerializeField] private Transform frontLeftWheelGameObject;
[SerializeField] private Transform frontRightWheelGameObject;
[SerializeField] private Transform rearLeftWheelGameObject;
[SerializeField] private Transform rearRightWheelGameObject;
private Rigidbody carRigidbody;
private float brakeForceVar;
private void Start()
{
brakeForceVar = brakeForce;
carRigidbody = GetComponent<Rigidbody>();
}
private void FixedUpdate()
{
GetInput();
HandleMotor();
HandleSteering();
UpdateWheels();
}
private void GetInput()
{
horizontalInput = Input.GetAxisRaw(horizontal);
verticalInput = Input.GetAxisRaw(vertical);
isBreaking = Input.GetKey(KeyCode.Space);
}
private void HandleMotor()
{
frontLeftWheelCollider.motorTorque = verticalInput * motorForce;
frontRightWheelCollider.motorTorque = verticalInput * motorForce;
brakeForce = isBreaking ? brakeForceVar : 0f;
ApplyBreaking();
}
private void ApplyBreaking()
{
frontRightWheelCollider.brakeTorque = brakeForce;
frontLeftWheelCollider.brakeTorque = brakeForce;
rearLeftWheelCollider.brakeTorque = brakeForce;
rearRightWheelCollider.brakeTorque = brakeForce;
}
private void HandleSteering()
{
steerAngle = maxSteerAngle * horizontalInput;
frontLeftWheelCollider.steerAngle = steerAngle;
frontRightWheelCollider.steerAngle = steerAngle;
}
private void UpdateWheels()
{
UpdateSingleWheel(frontLeftWheelCollider, frontLeftWheelTransform, frontLeftWheelGameObject);
UpdateSingleWheel(frontRightWheelCollider, frontRightWheelTransform, frontRightWheelGameObject);
UpdateSingleWheel(rearLeftWheelCollider, rearLeftWheelTransform, rearLeftWheelGameObject);
UpdateSingleWheel(rearRightWheelCollider, rearRightWheelTransform, rearRightWheelGameObject);
}
private void UpdateSingleWheel(WheelCollider wheelCollider, Transform wheelTransform, Transform wheelTransformGameObject)
{
Vector3 pos;
Quaternion rot;
wheelCollider.GetWorldPose(out pos, out rot);
wheelTransformGameObject.rotation = rot;
wheelTransform.position = pos;
}
void OnCollisionEnter(Collision col)
{
if(col.gameObject.tag == "CanBeHit")
{
print("LOSE");
}
}
}
Please let me know what I can do to fix this issue.