Player's rotation gets stuck when pressing two movement keys together, Player's Rotation breaks when pressing two movement buttons together
So I'm making a simple game, where the player controls a car that can only turn around on the X-axis, while the textures of the world are constantly moving, so that it creates an effect that the car is moving forward while in fact it isn't. I also added a feature via Quaternion.Slerp that turns the car on the Y-axis, so that it would create an effect of an actual turning car, just like a real-life car would turn. The problem is that the Y-axis rotation can get stuck on a set angle if I press two buttons together, so it looks like the car is drifting non-stop. Obviously I don't want that, however I don't really know how to fix that. I've tried looking up an answer but i came up empty.
public class PlayerController : MonoBehaviour
{
public Quaternion currentAngle;
Quaternion targetAngleBegining = Quaternion.Euler(0f, 0f, 0f);
Quaternion targetAngleEndRight = Quaternion.Euler(0f, 30.0f, 0f);
Quaternion targetAngleEndLeft = Quaternion.Euler(0f, -30.0f, 0f);
private float xTurnLimit = 8.0f;
public float turnSpeed;
private void Start()
{
currentAngle = targetAngleBegining;
}
private void Update()
{
MovePlayer();
ClampPlayerTurn();
}
private void MovePlayer()
{
Vector3 rotationOrigin = Vector3.zero;
float playerMovement = Input.GetAxis("Horizontal");
transform.Translate(Vector3.right * playerMovement * turnSpeed * Time.deltaTime, Space.World);
if(Input.GetKeyDown(KeyCode.D))
{
if(currentAngle.eulerAngles.y==targetAngleBegining.eulerAngles.y)
{
currentAngle = targetAngleEndRight;
}
else
{
currentAngle = targetAngleBegining;
}
}
if(Input.GetKeyUp(KeyCode.D))
{
if (currentAngle.eulerAngles.y == targetAngleEndRight.eulerAngles.y)
{
currentAngle = targetAngleBegining;
}
else
{
currentAngle = targetAngleEndRight;
}
}
if(Input.GetKeyDown(KeyCode.A))
{
if(currentAngle.eulerAngles.y==targetAngleBegining.eulerAngles.y)
{
currentAngle = targetAngleEndLeft;
}
else
{
currentAngle = targetAngleBegining;
}
}
if (Input.GetKeyUp(KeyCode.A))
{
if (currentAngle.eulerAngles.y == targetAngleEndLeft.eulerAngles.y)
{
currentAngle = targetAngleBegining;
}
else
{
currentAngle = targetAngleEndLeft;
}
}
this.transform.rotation = Quaternion.Slerp(this.transform.rotation, currentAngle, 0.010f);
}
private void ClampPlayerTurn()
{
if(transform.position.x < -xTurnLimit)
{
transform.position = new Vector3(-xTurnLimit, transform.position.y, transform.position.z);
}
else if(transform.position.x > xTurnLimit)
{
transform.position = new Vector3(xTurnLimit, transform.position.y, transform.position.z);
}
}
}
Here's my code. It's not pretty, but it works somehow. I'm always open to suggestions and criticism. Thanks in advance if you've replaied to my question.