Detect when rotation stops

Got a cannon that I move with transform following the mouse pointer. My problem is that I can’t detect when the cannon stops rotating. It doesn’t have any angular velocity etc probably since it moves with transform. Any way to make an “if cannon doesn’t rotate” statement?

Cannons! sounds cool :smiley: How about something like this?

    public Quaternion rotationStored;
	public Quaternion currentRotation;

	public float maxErrorAllowedPerFrame;

	public bool inRange;

	// Use this for initialization
	void Start () {
		rotationStored = transform.rotation;
		currentRotation = transform.rotation;
	}
	
	// Update is called once per frame
	void Update () {

		currentRotation = transform.rotation;

		if (Mathf.Abs (Quaternion.Angle (rotationStored, transform.rotation)) < maxErrorAllowedPerFrame || currentRotation == rotationStored) {
			
			inRange = true;

		} else {
			
			inRange = false;

		}

		rotationStored = transform.rotation;

		if (inRange) {

			// do somethin'

		}

	}

	// Keith Rek, 2018-07-26.

Tested on my Editor, and it worked (almost) perfectly.

I found out that both the ‘Quaternion.Angle’ function AND the ‘Vector3.Angle’ function was NOT accurate, both of them returning an error of 0.02 or so, even when the two rotation values were exactly the SAME.

So, I came up with a new condition

     currentRotation == rotationStored

in order to cover up the function’s inaccuracy.

     public Quaternion rotationStored;
     public Quaternion currentRotation;

‘rotationStored’ is the value that gets stored on every end of the frame, and the ‘currentRotation’ is the value that gets stored at the start of it. The ‘Quaternion.Angle’ function will print out the difference in angle between the two 'Vector’s, and ‘Mathf.Abs’ function will turn them into an ‘Absolute’ value.

     public float maxErrorAllowedPerFrame;
 
     public bool inRange;

You can tinker with the ‘maxErrorAllowedPerFrame’ variable to set its responsiveness against the detection or so, and finally, the boolean value ‘inRange’ will turn true when there is no rotation detected. The rest is up to you.

Yes,Assuming its 3d (if 2D game check z value) game There is way get trasform.rotation.y value and put if condition for ex.

if(trasform.rotation.y>=90 or 120 or whatever you want)

{

// do someting

}