Rotate object 180 on y axis in Update

I’m trying to flip a playing card in the Update() method by rotating it on the y axis.

The below code works but wanted to see if there was a better way to accomplish this.

void Update()
{
    if (isFlipping)
    {
        transform.Rotate(0, flipSpeed * Time.deltaTime, 0);
    }
	
	if (isFaceUp && transform.localEulerAngles.y < 180)
	{
		isFlipping = false;
	}
	else if (!isFaceUp && transform.localEulerAngles.y > 180)
	{
		isFlipping = false;
	}
}


public void FlipCard()
{
    if (!isFlipping)
    {
        isFaceUp = !isFaceUp;
        isFlipping = true;
    }
} 

If you orientate the card so that 90 is facing up and -90 is facing down you can do this:

float targetRot=90; // 90 is facing up. -90 is facing down

	void Update()
	{
		transform.rotation=Quaternion.RotateTowards(transform.rotation,Quaternion.Euler(0,targetRot,0),200*Time.deltaTime);
		if (Input.GetKeyDown(KeyCode.Q))
			targetRot=-targetRot; // flip the card
	}
1 Like

Personally, I would just use something like the free DOTween asset and then you can just call transform.DORotate(Vector3.up, timeToRotateInSeconds) or transform.DORotate(Vector3.down, timeToRotateInSeconds). It lets you fire-and-forget all sorts of effects, and you can adjust settings like changing the easing, creating sequences of effects, and more.

This worked great, ty!