0
\$\begingroup\$

i'm trying to limit maximum rotation angle and i do not know how to do that.. i am using this code currently, how can i limit maximum rotation angle of object?

    using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class rotObj : MonoBehaviour {

    void Update () {
        var dir = Camera.main.ScreenToWorldPoint (Input.mousePosition) - transform.position;
        var angle = Mathf.Atan2 (dir.y, dir.x) * Mathf.Rad2Deg;
        var rot = Quaternion.AngleAxis (angle, Vector3.forward);
        transform.rotation = rot;
    }

}
\$\endgroup\$
1
  • \$\begingroup\$ What is the definition of maximum rotation angle for you? \$\endgroup\$ Mar 31, 2018 at 22:43

1 Answer 1

0
\$\begingroup\$

What you have to do is just check if the rotation is bigger than the rotation that you want to be max. If it's bigger, then set it to max and if it's not then just leave it. Code should be something like:

currentRotation = Mathf.Min (currentRotation, maxRotation);

This will take the smaller one. If you are limiting it to mininum rotation. Instead use:

currentRotation = Mathf.Max (currentRotation, minRotation);

I haven't tested this code but i think it should work!

\$\endgroup\$
1
  • \$\begingroup\$ thanks, i kinda got what i wanted to do and your asnwer helped me, i used this to "fix" my problem. private float maxRotaion = 35f; if (angle >= -maxRotaion && angle <= maxRotaion) transform.rotation = Quaternion.AngleAxis (angle, Vector3.forward); \$\endgroup\$
    – Nikka
    Mar 31, 2018 at 11:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .