Hello, thank you to anyone in advance who responds to this request. I have been working on a 2D action RPG game and have created a script to allow my character’s arms to move as a separate object. I am using Quaternions for the first time, but I have successfully made the arms track the position of the mouse. To limit the rotation I used Mathf.Clamp with a range. This worked correctly when the character is looking forwards because the range is -90 to 90. Now when the object is flipped I used a rotation offset to change the rotZ value so that it would properly follow the mouse, this value was 180. Now the problem is when it is flipped the rotZ goes from 90 to 180 and then -179.9999 to -90. Is there anyway for me to check the absolute value of rotZ so I can make a range of 90 to 180 without effecting the rotation? The code will be down below and once again thank you to anyone who is willing to help.
public class arm_controller : MonoBehaviour
{
public int rotationOffset;
public Vector3 pos;
public float zRotation;
public float rotzMax;
public float rotzMin;
public float rotZ;
public character_Controller theCharacter;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
difference.Normalize();
rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
rotZ = Mathf.Clamp(rotZ, rotzMin, rotzMax);
if (theCharacter.isFacingForward == true)
{
rotzMax = 90;
rotzMin = -90;
transform.rotation = Quaternion.Euler(0f, 0f, rotZ);
}
else if (theCharacter.isFacingForward == false)
{
transform.rotation = Quaternion.Euler(0f, 0f, rotZ + rotationOffset);
}
}
}