My Rotation solution

Hi guys,
this is my solution to the Unity5 rotation issue(this code is the IsStanding function):

The Code

Let’s assume that I have a threshold of 10. In that case I need to check that my object rotation is between 350 and 10 (both degrees). How can we do it?
Simple: if the number is greater than the minumum rotation (in degrees … so 350), we have to subtract that minimum rotation.

e.g. #1
the threshold is 10 and the rotation is 2.
Is 2 greater than 350? No, so no subtraction.
Is 2 lower than 10? Yes, so the pin is standing.

e.g. #2
the threshold is 10 and the rotation is 358.
Is 358 greater than 350? Yes, let’s do the subtraction…358 becomes 8.
Is 8 lower than 10? Yes, so the pin is standing.

e.g. #3
the threshold is 10 and the rotation is 13.
Is 13 greater than 350? No, so no subtraction.
Is 13 lower than 10? No, so the pin is NOT standing.

e.g. #4
the threshold is 10 and the rotation is 340.
Is 340 greater than 350? No, so no subtraction.
Is 340 lower than 10? No, so the pin is NOT standing.

Hope you will like it! :wink:

1 Like

The code worked well for me so thank you for providing this.

Only changes I made were to.

  1. Do the minRotation calculation in the Awake function rather than every update.
  2. Secondly, this meant I needed to declare the minRotation variable for the entire class.

Thanks again.
Lee

Another option is to use Quaternion.Angle which returns the angle between two rotations:

    Quaternion initialRot;
	void Start () 
	{
		initialRot = transform.rotation;
		
	}
	bool isStanding(Quaternion rot, float threshold)
	{
		if (Quaternion.Angle (initialRot, rot) >= threshold)
			return false;
		return true;
	}

	void Update () 
	    {
		if (!isStanding(transform.rotation,15))
			print("has fallen");
	    }

Unity uses quats internally to handle rotations, Vector3 isn’t ideal for that.

Privacy & Terms