public class CarController : MonoBehaviour
{
private const string Horizontal = "Horizontal";
private const string Vertical = "Vertical";
public float horizontalInput;
public float verticalInput;
public float speed;
Vector3 rotateRight = new Vector3(0, 30, 0);
Vector3 rotateLeft = new Vector3(0, -30, 0);
[SerializeField] private float motorForce;
[SerializeField] private Rigidbody rb;
[SerializeField] WheelCollider frontRight;
[SerializeField] WheelCollider frontLeft;
[SerializeField] WheelCollider rearRight;
[SerializeField] WheelCollider rearLeft;
private void FixedUpdate()
{
GetInput();
HandleDownForce();
HandleMotor();
HandleSteering();
}
private void HandleMotor()
{
if (Input.GetKey(KeyCode.W))
{
transform.position += Vector3.forward * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.S))
{
transform.position += Vector3.back * speed * Time.deltaTime;
}
}
private void HandleSteering()
{
if (Input.GetKey(KeyCode.D))
{
Quaternion deltaRotationRight = Quaternion.Euler(rotationRight * Time.deltaTime);
rb.MoveRotation(rb.rotation * deltaRotationRight);
}
if (Input.GetKey(KeyCode.A))
{
Quaternion deltaRotationLeft = Quaternion.Euler(rotationLeft * Time.deltaTime);
rb.MoveRotation(rb.rotation * deltaRotationLeft);
}
}
}
This is my current code, when moving forwards and backwards as well as left and right, it moves and turns on the axis. However, this means that when i turn, my car still moves along the axis whilst facing a different direction.
Is anyone able to help?