Problem in steering with wheelcolliders
Greetings, I'm making a Highway-Racer-type game and am using wheel colliders for car movement. However, I want to steer the car in such a way that as soon as the "A" or "D" key is released, the car straightens up according to the path, and not essentially steer in the same direction, just like typical highway racer games.
I tried to implement the same by turning all rotation axis values to 0 when the "A" and "D" keys are released. However, it doesn't seem natural as I want to gradually turn the car from its current direction to that of the path. Also, my code messes up the general steering of the car too such that it doesn't move as expected.
This is my code:-
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CarController : MonoBehaviour
{
private float horizontalInput;
private float currentSteerAngle;
private float vel = 3f;
[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;
private Rigidbody rb;
private Transform tf;
private ConstantForce cf;
// Start is called before the first frame update
void Start()
{
tf = GetComponent<Transform>();
rb = GetComponent<Rigidbody>();
cf = GetComponent<ConstantForce>();
}
// Update is called once per frame
void Update()
{
GetInput();
HandleSteering();
CheckRotation();
UpdateWheels();
}
private void GetInput()
{
horizontalInput = Input.GetAxis("Horizontal");
if (!(Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D)))
StraightenCar();
}
private void HandleSteering()
{
currentSteerAngle = maxSteerAngle * horizontalInput * vel;
frontLeftWheelCollider.steerAngle = currentSteerAngle;
frontRightWheelCollider.steerAngle = currentSteerAngle;
}
private void UpdateWheels()
{
UpdateSingleWheel(frontLeftWheelCollider, frontLeftWheelTransform);
UpdateSingleWheel(frontRightWheelCollider, frontLeftWheelTransform);
}
private void UpdateSingleWheel(WheelCollider wheelCollider, Transform wheelTransform)
{
Vector3 pos;
Quaternion rot;
wheelCollider.GetWorldPose(out pos, out rot);
wheelTransform.rotation = rot;
wheelTransform.position = pos;
}
private void CheckRotation()
{
var rotation = tf.eulerAngles;
rotation.y = ClampAngle(rotation.y, -30, 30);
tf.eulerAngles = rotation;
}
private float ClampAngle(float angle, float min, float max)
{
if (angle < 90 || angle > 270)
{
if (angle > 180) angle -= 360;
if (max > 180) max -= 360;
if (min > 180) min -= 360;
}
angle = Mathf.Clamp(angle, min, max);
if (angle < 0) angle += 360;
return angle;
}
private void StraightenCar()
{
Vector3 rotation = tf.eulerAngles;
rotation.x = 0;
rotation.y = 0;
rotation.z = 0;
tf.eulerAngles = rotation;
}
}
Note: To sum up, I need help in:-
(1) Straighten car after moving left or right.
(2) Make the straightening transition smooth.
Thank you very much in advance. I really appreciated even the shortest of replies.
Your answer
Follow this Question
Related Questions
unity 5 car tutorial 3D 1 Answer
Wheel Colliers sinking into ground 1 Answer
simple car drift system 0 Answers
Why my car is jumping like that 0 Answers
changing rpm when shifting 1 Answer