- Home /
Problem with Constraint Camera Rotation C#
using UnityEngine;
using System.Collections;
public class ConstraintRotation : MonoBehaviour {
public enum ConstraintAxis{X = 0, Y, Z};
public ConstraintAxis axis; // Rotation around this axis is constrained
public float min; // Relative value in degrees
public float max; // Relative value in degrees
private Transform thisTransform;
private Vector3 rotateAround;
private Quaternion minQuaternion;
private Quaternion maxQuaternion;
private float range;
void 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( 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
void 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;
}
}
}
this is my code, and the Console is saying some errors
The best overloaded method match for UnityEngine.Vector3.this[int]' has some invalid arguments Argument
#1' cannot convert ConstraintRotation.ConstraintAxis' expression to type
int'
The best overloaded method match for `UnityEngine.Quaternion.AngleAxis(float, UnityEngine.Vector3)' has some invalid arguments
Can someone help me? It's a script to Constraint X rotation of the main camera.
Answer by robertbu · Feb 12, 2014 at 08:58 PM
Just cast axis to an int everywhere you use it as an index in an array:
euler[ (int)axis ] = maxQuaternion.eulerAngles[ (int)axis ];
You also have a '.AngleAxis' missing on line 37.
Answer by PicturesInDark · May 20, 2014 at 05:27 PM
For this to work: public enum ConstraintAxis{X = 0, Y = 1, Z = 2};
The script works ok!
Answer by leandroreschke · May 22, 2014 at 10:53 PM
Robertbu already resolved it for me, but your answer is valid if I wanted to use y and z, I made this from a js script, this is why my code has problems, forgot to set INT. But thanks!
Your answer
Follow this Question
Related Questions
really annoying rotation problem. 0 Answers
Changing this code to control camera with keys? 0 Answers
Rotating an already rotated object. 1 Answer