Rotating between 2 extreme angles in Unity C#

Ok, so this is likely a really basic solve, but I can't for the life of me puzzle it out. I'm trying to rotate an object in 2D up to positive or negative 10 degrees, depending on the direction of movement (right or left). At this stage my code looks like this :

void Update () 
	{
		if(MoveMods.driftRight && transform.rotation.z >= 350)
		{
			transform.Rotate(0,0,-1);
		}else if (!MoveMods.driftRight && transform.rotation.z <= 10)
		{
			transform.Rotate(0,0,1);
		}
}

This just rotates the object eternally in one direction or the other - it does change direction, but it needs to stop. Oh won't you please, please help me (help me, help ooooooooooh).

Comments

  • Your problem is that you are treating your rotation as if it was a Vector3, it's actually a four dimensional vector called a quaternion. The three numbers you see in the inspector are the Euler angle representation of said quaternion. So you can use that instead:

    transform.rotation.eulerAngles.z

  • Fantatsic, thank you kindly. It seems I have another issue now though.
    void Update () 
    	{
    		if(MoveMods.driftRight && (transform.rotation.eulerAngles.z >= 350 || transform.rotation.eulerAngles.z <= 10))
    		{
    			transform.Rotate(0,0,-0.5f);
    
    		}else if (!MoveMods.driftRight && (transform.rotation.eulerAngles.z <= 10 || transform.rotation.eulerAngles.z >=350))
    		{
    			transform.Rotate(0,0,0.5f);
    		}


    The object now gets stuck after rotating to one of the limits, because (I think) that within the update, it exceeds the limit by some tiny value, and therefore lies out of range. I can't see any way around this at this stage.
  • I changed your code a little for clean up, then I tried clamping the angle, but since we are moving over the 0 degree threshold we needed a custom clamp. I found a solution here that is perfect and added it to your code. See below, hope it helps :)

    void Update () {

    if (transform.eulerAngles.z >= 350 || transform.eulerAngles.z <= 10) {
    if(MoveMods.driftRight) {
    transform.Rotate(0,0,-0.5f);
    } else {
    transform.Rotate(0,0,0.5f);
    }
    }

    float rotZ = ClampAngle(transform.eulerAngles.z, -10, 10);
    transform.eulerAngles = new Vector3(0, 0, rotZ);
    }

    private float ClampAngle(float angle, float min, float max) {

    if (angle < 90 || angle > 270) {
    if (angle > 180) angle -= 360;
    if (max > 180) max -= 360;
    if (min > 180) min -= 360;
    }

    angle = Mathf.Clamp(angle, min, max);

    if (angle < 0) angle += 360;

    return angle;
    }
    Thanked by 1Bracula
  • That is incredible. @Squidcor @Thelangfordian Thank you so much, both of you. I really really appreciate it. Works perfectly!
    Thanked by 1Thelangfordian
  • I had the same issue while trying to rotate an object between certain angles in my game, and I solved it with help of this topic. Thank you @Bracula for your question, and thank you @Thelangfordian for the clear answer.
Sign In or Register to comment.