Comments and answers for "How can I set a limit to my object rotation???(Here's the code)"
http://answers.unity.com/questions/56168/how-can-i-set-a-limit-to-my-object-rotationheres-t.html
The latest comments and answers for the question "How can I set a limit to my object rotation???(Here's the code)"Answer by Statement
http://answers.unity.com/answers/56175/view.html
<p>An alternative approach to the same problem.</p>
<pre><code>var velocity = 3.0;
var minXAngle : float = -45;
var maxXAngle : float = 45;
var minZAngle : float = -45;
var maxZAngle : float = 45;
private var xAngle : float = 0.5f;
private var zAngle : float = 0.5f;
function Update () {
var vdt = velocity * Time.deltaTime;
var xtrans = Input.GetAxis("Horizontal") * vdt;
var ytrans = Input.GetAxis("Vertical") * vdt;
var ztrans = Input.GetAxis("Fire1") * vdt;
transform.Translate(-ytrans, ztrans, xtrans, Space.World);
Rotate(xtrans, ytrans);
}
function Rotate (x : float, z : float) {
xAngle = Mathf.Clamp01(xAngle + x);
zAngle = Mathf.Clamp01(zAngle + z);
var euler = transform.eulerAngles;
euler.x = Mathf.Lerp(minXAngle, maxXAngle, xAngle);
euler.z = Mathf.Lerp(minZAngle, maxZAngle, zAngle);
transform.eulerAngles = euler;
}
</code></pre>Sun, 03 Apr 2011 13:10:59 GMTStatementAnswer by Peter G
http://answers.unity.com/answers/56172/view.html
<p>You can try directly testing EulerAngles, but that will give you issues so you should probably try storing them in your own variables. You could do it using transform.Rotate(), but you would need several conditionals to do all the checks and I don't like how it looks. So this is method is different but <em>should</em> do the same thing.</p>
<pre><code>var velocit : float;
//Change the value of these two fields to control the max angles.
var maxRotX = 90.0;
var maxRotY = 180.0;
private var curRotX : float;
private var curRotY : float;
function Update () {
//This is all still the same.
var xtrans = Input.GetAxis("Horizontal") * velocit * Time.deltaTime;
var ytrans = Input.GetAxis("Vertical") * velocit * Time.deltaTime;
var ztrans = Input.GetAxis("Fire1") * velocit * Time.deltaTime;
transform.Translate(-ytrans,ztrans,xtrans,Space.World);
//Set the curRot_ to the curRot_ + the _trans and clamp it between the min and max so we will never be larger than some angle.
curRotX = Mathf.Clamp(curRotX + xtrans, -maxRotX, maxRotX);
curRotY = Mathf.Clamp(curRotY + ytrans, -maxRotY, maxRotY);
transform.rotation = Quaternion.Euler(curRotX, 0, curRotY);
//This constructs an angle (x, y, z) and assigns it to the rotation.
//EDIT:
//And you called it ytrans, but you put it in the z pos so I kept that convention.
}
</code></pre>
<p>The problem with reading the euler angles directly is that Unity internally stores rotations as quaternions. When it converts them to euler angles, there are multiple values is can give. For example, 90 degrees is the same as -270 degrees. And Unity occasionally changes the value it gives you.</p>Sun, 03 Apr 2011 12:48:47 GMTPeter G