Prachi Joshi
Prachi Joshi

Reputation: 375

how to limit the rotation of a game object in unity3d

I am making a game where there is a tank that shoots some objects. I want to rotate the gun of the tank in between the llimits of -90 to 90 degrees in "X". I used transform.rotate but it rotates the gun with some float values (0.0 to 1.0) and the rotation I see on the inspector is something different. How to solve this issue?

Upvotes: 1

Views: 10949

Answers (2)

David
David

Reputation: 16287

Use below code from Mathf class

static float Clamp(float value, float min, float max); 

e.g.

var angle = Mathf.Clamp(angle, 90, 270);
Quaternion target = Quaternion.Euler(0, 0, angle); // any value as you see fit
transform.rotation = target;

Upvotes: 4

Prachi Joshi
Prachi Joshi

Reputation: 375

I found the answer I used eularAngles to limit the rotations as follows

 if((gun.eulerAngles.z<90)||(gun.eulerAngles.z>270))
        {
            gun.Rotate(0,0,turnAngle);
        }

Upvotes: 0

Related Questions