- Home /
Need to limit the rotation angles on a turret
So I have a turret on a fixed surface. I need to keep it from moving past certain angles.
See this image:[1]
[1]: /storage/temp/2184-rotationexample.jpg
My rotation degrees are probably incorrect in the context of Unity, though they are just there so I can better explain my issue.
I need the turret (the black box in the image) to only be able to rotate so that it's facing anywhere between 90 and -90 degrees (in the context of the image). I have a feeling that using a Mathf.Clamp function may be the answer here, but the function itself has kind of confused me, so some further explanation on how it works would be very much appreciated.
I prefer answers to be in C#, though I can probably translate from Javascript.
EDIT: I should also probably add in that the turret starts off facing towards 0 degrees (again, in the context of the image).
EDIT 2: Here is my code for rotating the base of the turret:
//for rotating the turret base
transform.Rotate(new Vector3(0, Input.GetAxis("Mouse X") * rotateSpeed, 0) * Time.deltaTime);
And my code for tilting the gun up and down:
gun.transform.Rotate(new Vector3(Input.GetAxis("Mouse Y") * rotateSpeed, 0, 0) * Time.deltaTime);
Answer by supernat · Jul 27, 2012 at 07:05 AM
Do you have code that performs the rotation, or are you utilizing a third party utility? Somewhere, there's code to allow the rotation similar to the following:
// If left mouse down, rotate left, else rotate right, for example
if (left)
transform.localEulerAngles = transform.localEulerAngles + new Vector3(0.0f, Time.deltaTime*rotateRate, 0.0f);
else if (right)
transform.localEulerAngles = transform.localEulerAngles + new Vector3(0.0f, -Time.deltaTime*rotateRate, 0.0f);
In this example, you are rotating the turret left or right. To clamp it, you could instead:
Vector3 angle = transform.localEulerAngles;
angle.y = Mathf.Clamp(angle.y + Time.deltaTime*rotateRate, -90.0f, 90.0f);
transform.localEulerAngles = angle;
The clamp function simply enforces that the final value is between the min and max (the min is the second parameter and max is the third).
rotateRate here is how many degrees per second you allow the turret to rotate. You only rotate around the angle.y as the Y axis is your UP axis (in this example).
I use the localEulerAngles because if the turret is sitting on a ship that is swaying left/right such that the World coordinate Y axis is not aligned with the turret's Y axis, the turret will rotate around the Y axis of the parent (ship) instead. If you never rotate your ship around the X or Z axes, you could use the eulerAngles instead of localEulerAngles. Hope that makes sense.
I actually do have some code to do the rotation. I'll stick it in the question after I finish posting this. But for the time being, here it is:
//for rotating the turret base transform.Rotate(new Vector3(0, Input.GetAxis("$$anonymous$$ouse X") rotateSpeed, 0) Time.deltaTime);
I should add that my turret is split into two parts, a base and the gun itself. The base will only turn left and right and the gun will only tilt up and down. What I've done is I've parented the turret base to the ship and the gun to the turret base, so the line of parenting is as follows:
playerShip --> turretBase --> gun
Additionally, this is my code for tilting the gun up and down: gun.transform.Rotate(new Vector3(Input.GetAxis("$$anonymous$$ouse Y") rotateSpeed, 0, 0) Time.deltaTime);
Your answer
![](https://koobas.hobune.stream/wayback/20220613074749im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Play Sound when turret rotates 1 Answer
Multiple Cars not working 1 Answer
Limiting Local Turret Rotation 0 Answers
Turret Continuing to Max Rotation - Following Mouse Cursor 0 Answers