How to properly set rotation constraints in the positive and negative direction?
The code I'm working on involves rotating an object when a button is pressed, and there is a case set up so that it will store the transform data until the next time a key is pressed, then change it again. The portion of the code locking the rotation from going past 60 degrees in the z-axis works fine, but the portion locking rotation @ -60 degrees doesn't. It seems to reset the position at the initial pos (z_init in this case) + 60 as opposed to z_init - 60. I've been struggling with the logic for this so I'm reaching out. Any advice?
float j = Input.GetAxis("Vertical") * 100 * Time.deltaTime;
Vector3 shoulderRotUD = new Vector3(x, 0, z + j);
float check_z = z_init - shoulderRotUD.z; //z rotation constraint variable
float diff_z = 0 + shoulderRotUD.z; //calculate z rotation angle for constraints
if (check_z > 60f || check_z < -60f) //z constraints
{
if (diff_z <= -60f)
{
shoulderRotUD = new Vector3(x, 0, z_init - 59.99f); //set for return to z
}
if (diff_z >= 60f)
{
shoulderRotUD = new Vector3(x, 0, z_init + 59.99f); //set for return to z
}
}
Your answer
Follow this Question
Related Questions
Make RawImage on canvas look at gameObject 0 Answers
Rotat a GameObject and than shoot it in the New (x) direction 1 Answer
How to determine one point (Vector3) based on 7 other points (Vector3's) with 2 coordinate systems? 1 Answer
Rotate a vector based on a direction (transform.up) of a game object 1 Answer