Rotation Constraint

Hi,

I’m using this code:

// Revelopment.co.uk
// Created by Carlos Revelo
// 2011




/********Main Objects***********/

var targetItem : GameObject;
var GUICamera : Camera;
var ambient : GameObject;


/********Rotation Variables*********/
var rotationRate : float = 1.0;
private var wasRotating;

/************Scrolling inertia variables************/
private var scrollPosition : Vector2 = Vector2.zero;
private var scrollVelocity : float = 0;
private var timeTouchPhaseEnded: float;
private var inertiaDuration : float = 0.5f;

private var itemInertiaDuration : float = 1.0f;
private var itemTimeTouchPhaseEnded: float;

private var rotateVelocityY : float = 0;


var hit: RaycastHit;

private var layerMask = (1 <<  8) | (1 << 2);



function Start()
{
	layerMask =~ layerMask;	
}

function FixedUpdate()
{
	
	if (Input.touchCount > 0) 
	{		//	If there are touches...
			var theTouch : Touch = Input.GetTouch (0);		//	Cache Touch (0)
			
			var ray = Camera.main.ScreenPointToRay(theTouch.position);
			//var GUIRay = GUICamera.ScreenPointToRay(theTouch.position);
			
				
         	if(Physics.Raycast(ray,hit,50,layerMask))
         	{	

                                               if(Input.touchCount == 1)
						{
							
							if (theTouch.phase == TouchPhase.Began) 
         					{
         						wasRotating = false;	
         					}		
         					
         					if (theTouch.phase == TouchPhase.Moved) 
         					{
          		        		
         						targetItem.transform.Rotate(theTouch.deltaPosition.y * rotationRate, -theTouch.deltaPosition.x * rotationRate,0,Space.World);
         						wasRotating = true;
         					}		
         	
         					if (theTouch.phase == TouchPhase.Ended || theTouch.phase == TouchPhase.Canceled) 
         					{
         						if(wasRotating==true)
         					
         							
           							if(Mathf.Abs(theTouch.deltaPosition.y) >=10)
         							{
         								rotateVelocityY = theTouch.deltaPosition.y / theTouch.deltaTime;
           							}	
         						itemTimeTouchPhaseEnded = Time.time;
         						}
                                                   }
						}
			}


			
						
			
	}

I cut some of it out and it still works. I want to constrain rotation to only the Y axis. I applied this code:

//////////////////////////////////////////////////////////////
// RotationConstraint.js
// Penelope iPhone Tutorial
//
// RotationConstraint constrains the relative rotation of a 
// Transform. You select the constraint axis in the editor and 
// specify a min and max amount of rotation that is allowed 
// from the default rotation
//////////////////////////////////////////////////////////////

enum ConstraintAxis
{
	X = 0,
	Y
	Z, 
}

public var axis : ConstraintAxis; 			// Rotation around this axis is constrained
public var min : float;						// Relative value in degrees
public var max : float;						// Relative value in degrees
private var thisTransform : Transform;
private var rotateAround : Vector3;
private var minQuaternion : Quaternion;
private var maxQuaternion : Quaternion;
private var range : float;

function Start()
{
	thisTransform = transform;
	
	// Set the axis that we will rotate around
	switch ( axis )
	{
		case ConstraintAxis.X:
			rotateAround = Vector3.right;
			break;
			
		case ConstraintAxis.Y:
			rotateAround = Vector3.up;
			break;
			
		case ConstraintAxis.Z:
			rotateAround = Vector3.forward;
			break;
	}
	
	// Set the min and max rotations in quaternion space
	var axisRotation = Quaternion.AngleAxis( thisTransform.localRotation.eulerAngles[ axis ], rotateAround );
	minQuaternion = axisRotation * Quaternion.AngleAxis( min, rotateAround );
	maxQuaternion = axisRotation * Quaternion.AngleAxis( max, rotateAround );
	range = max - min;
}

// We use LateUpdate to grab the rotation from the Transform after all Updates from
// other scripts have occured
function LateUpdate() 
{
	// We use quaternions here, so we don't have to adjust for euler angle range [ 0, 360 ]
	var localRotation = thisTransform.localRotation;
	var axisRotation = Quaternion.AngleAxis( localRotation.eulerAngles[ axis ], rotateAround );
	var angleFromMin = Quaternion.Angle( axisRotation, minQuaternion );
	var angleFromMax = Quaternion.Angle( axisRotation, maxQuaternion );
		
	if ( angleFromMin <= range  angleFromMax <= range )
		return; // within range
	else
	{		
		// Let's keep the current rotations around other axes and only
		// correct the axis that has fallen out of range.
		var euler = localRotation.eulerAngles;			
		if ( angleFromMin > angleFromMax )
			euler[ axis ] = maxQuaternion.eulerAngles[ axis ];
		else
			euler[ axis ] = minQuaternion.eulerAngles[ axis ];

		thisTransform.localEulerAngles = euler;		
	}
}

It seems that the touch script ignores this. Any ideas?

How can I constrain the swipe motion to only the Y axis of rotation? Thanks.