Problem with clamping?

Okay so I have this script attached to an empty gameobject that serves as a parent for two child objects underneath, what I want to do is to rotate the parent object which works perfectly fine until I try to clamp the rotation. Here’s what I’ve got so far:

#pragma strict


var rotSpeed : float = 5.0;

var rotOn : boolean = false;


function Update () {

	if(Input.GetButton("Jump" || rotOn == false)){
	
		rotOn = true;
		
			}
			
	if( rotOn == true){
	
		transform.Rotate(0,0,-rotSpeed * Time.deltaTime);
		
		transform.rotation.z = Mathf.Clamp(transform.rotation.z, 360.0,270.0 );
		
		}

}

The reason why I set it to clamp between 360 and 270 degrees is because at runtime, -90 degrees is the same as 270 and I have no idea why, the clamping also makes my object “snap” to about 180 degrees and won’t move. What might be the problem? Is there a diferent kind of rotation at runtime than “outside” of runtime? Anything that will help me understand what I am doing wrong will most certainly be of help. Thanks in advance :slight_smile:

1 Answer

1

transform.rotation is stored as a Quaternion and does not vary between 0 and 360 (rather between -1 and 1 I think) and therefore clamping it between 270 and 360 actually only returns it as 1 which happens to be where the eulerian angle is 180… weird technical bit that I don’t really understand out the way

Try replacing:

transform.rotation.z = Mathf.Clamp(transform.rotation.z, 360.0,270.0 );

with:

transform.eulerAngles.z = Mathf.Clamp(transform.eulerAngles.z, 270, 360);

as a note you should always make sure the smaller number comes first in your Mathf.Clamp command as that will mess things up too.

Scribe

Ah! that made it work. I didn't know that rotation was stored as a Quaternion. Thanks!