Hey guys. I am trying to make it so that the player turns around 180 degrees (in a lerping fashion) when holding down B and turns back around (also in a lerping fashion) when let go of B. So far the first half works. The player turns around 180 degrees and stays in that direction as long as he holds B, but when he lets go the player instantly goes back to his starting rotation, and doesn’t lerp back like I want it to. I don’t know what is wrong with my function. (Btw all seemingly undeclared variables are actualy global variables. In case you were wondering.)
void TurnAroundLerp()
{
if (Input.GetButtonUp("B") == false)
{
if (Input.GetButtonDown("B"))
{
turnAroundStartRotation = transform.rotation;
turnAroundTargetRotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y + 180, transform.rotation.eulerAngles.z);
turnAroundStartTime = Time.time;
turnAroundCurrentTime = turnAroundStartTime;
}
else if (Input.GetButton("B"))
{
turnAroundCurrentTime += Time.deltaTime;
percentageDone = (turnAroundCurrentTime - turnAroundStartTime) / turnAroundLerpTime;
if (percentageDone < 1f)
{
transform.rotation = Quaternion.Lerp(turnAroundStartRotation, turnAroundTargetRotation, percentageDone);
}
else
{
Quaternion targetRotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y + 180, transform.rotation.eulerAngles.z);
transform.rotation = targetRotation;
}
}
}
else if (Input.GetButtonUp("B") == true)
{
percentageDone = 0;
turnAroundStartRotation = transform.rotation;
turnAroundTargetRotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y + 180, transform.rotation.eulerAngles.z);
turnAroundStartTime = Time.time;
turnAroundCurrentTime = turnAroundStartTime;
while (percentageDone < 1)
{
Debug.Log(percentageDone);
turnAroundCurrentTime += Time.deltaTime;
percentageDone = (turnAroundCurrentTime - turnAroundStartTime) / turnAroundLerpTime;
transform.rotation = Quaternion.Lerp(turnAroundStartRotation, turnAroundTargetRotation, percentageDone);
}
}
}