C#C
C#3y ago
henke

✅ C# unity rotation clamping.

So I want it to be able to rotate in a 180 degree angle from the left side and 180degree from the right side.. depending on where my mouse is at and which direction i'm facing. Basically limiting the angles the player can have depending on which side hes facing.
The right side (changeDirection.x == 1) works.
The left side (changeDireciton.x == -1) doesn't work.
Any help or code rewrite suggestion appreciated !
// private void Update()
    {

        Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        mousePos.z = 0f;

        Vector3 aimDirection = (mousePos - transform.position).normalized;
        

        if(changeDirection.x == -1)
        {
            angle = (Mathf.Atan2(aimDirection.y, aimDirection.x) * Mathf.Rad2Deg - 180f);
            angle = Mathf.Clamp(angle, -90f, 90f);
            print(angle);
        }
        else if(changeDirection.x == 1)
        {
            angle = (Mathf.Atan2(aimDirection.y, aimDirection.x) * Mathf.Rad2Deg);
            angle = Mathf.Clamp(angle, -90f, 90f);
            //print(Mathf.Rad2Deg);
        }

        zAngle = new Vector3(0, 0, angle);
        transform.eulerAngles = zAngle;
    }
Was this page helpful?