I’m trying to get a Motorcycle to lean left, right and return upright when no key is pressed. Not sure why this isn’t working. The main problem is the bike doesn’t return to a 0 Z rotation. Any ideas?
void BikeLean()
{
isPlayerTurning = false;
if (Input.GetKey("a"))
{
isPlayerTurning = true;
targetRotation = Quaternion.AngleAxis(15f, transform.forward) * transform.rotation;
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, smooth * Time.deltaTime);
Debug.Log("Turning Left");
}
else if (Input.GetKey("d"))
{
isPlayerTurning = true;
targetRotation = Quaternion.AngleAxis(-15f, transform.forward) * transform.rotation;
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, smooth * Time.deltaTime);
Debug.Log("Turning Right");
}
else // level bike if not turning
{
targetRotation = Quaternion.AngleAxis(0f, transform.forward) * transform.rotation;
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, smooth * Time.deltaTime);
}
}