- Home /
A better way than this to constrain angles?
Hey guys, I have made this script for my game. Basically it stops the bone from rotating too high and too low. It works which is a start but when I hit the angle specified, it jitters going back and forward. I know why it does this because I am saying if it is higher than this value, snap to that value. I am just wondering what I could do in order to make it stop when it hits the angle specified and not jitter, but when I want to move down, the bone moves again. Here is what I have so far:
var spine : Transform;
var rotateSpeed : int = 18;
function Update()
{
if (gunAiming.aimMode == 1 || gunAiming.aimMode == 0)
{
spine.transform.Rotate(Vector3(0, 0, Input.GetAxis("Mouse Y")) * Time.deltaTime * rotateSpeed);
}
if (spine.transform.rotation.eulerAngles.z > 294)
{
Debug.Log("294");
spine.transform.rotation.eulerAngles.z = 294;
}
if (spine.transform.rotation.eulerAngles.z < 244)
{
Debug.Log("244");
spine.transform.rotation.eulerAngles.z = 244;
}
}
Answer by OP_toss · Apr 11, 2013 at 10:50 PM
Keep a running variable of the current rotation. Add to the variable instead of rotating transform Clamp the variable between your limits Apply the variable to your transform like so:
transform.rotation = Quaternion.Euler( curRot )
Also, don't use Magic Numbers inside your code. Instead make vars at the top as constants in all caps. Mucho betterer.