- Home /
Can anyone help out form this problem please its urgent
I am very new to unity i want to restrict my first person controller to restrict the rotation in x-direction(i.e., horizontal direction). I just workout on a code i works to restrict the rotation in y-axis but i cant restrict it in x-axis direction please help me to solve this problem. My code is
enum ConstraintAxis
{
X,
Y,
Z
}
public var axis : ConstraintAxis; // Rotation around this axis is constrained
public var min : float = -20; // Relative value in degrees
public var max : float = 20; // 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;
}
// 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;
}
}
Answer by cdrandin · Jan 25, 2013 at 09:06 AM
Not to be picky and all, but I think constraint axis should be a class of its own. Like..
public class ConstraintAxis
{
public bool x;
public bool y;
public bool z;
}
Then call it like var freezeRotation : ConstraintAxis
, which will look just like the freezerotation in the inspector of a rigidbody. So you can do all your rotations and what not, BUT if one of the members in ConstraintAxis is true, restrict the movement from its default rotation. Where is this default rotation? In the start function save that data and you should be good to go.
So to show code:
//globals
var defaultRot : Quaternion;
...
public function Start (){
defaultRot = transform.rotation;
}
public function LateUpdate (){
...
//You currently have gotten all inputs, movement, rotations have been adjusted, before we send this info to Move(...)
if(ConstraintAxis.x)
transform.rotation.x = defaultRot.x
if(ConstraintAxis.y)
transform.rotation.y = defaultRot.y
if(ConstraintAxis.z)
transform.rotation.z = defaultRot.z
//call Move(...)
Not sure if this will work, but I think it would or at the very least the idea is there.
Ok I think I did the rotation write wrong. i.e transform.rotation.x
Um, trying to find out how to change the axis rotation right now..
O$$anonymous$$, you will need Transform.eulerAngles
so.. transform.eulerAngles.x
, but now I am not sure what type to store the defaultRot
so it will fit in nicely with eulerAngles.
Sorry I am all over the place a bit tired, but hopefully you have a general idea now.
Your answer
![](https://koobas.hobune.stream/wayback/20220613091547im_/https://answers.unity.com/themes/thub/images/avi.jpg)