- Home /
How to limit the rotation of an object
I have a code that allows me to rotate my tank tower and its barrel using UI Joystick. The controls part is working, the problem is that I can't limit the rotation of my barrel (I need to lock it on 15 degrees). I've tried literally everything that my newbie hands can, but nothing works.
The closest I could get to my goal is the thing you see in the code (I commented it). It works, kinda. When I start a game, the barrel's X axis locks on -15 degrees and when I try to rotate the barrel up, as it reaches 0 degrees it changes to -15 degrees back. Please help!
using UnityEngine;
public class TurretJoystick : MonoBehaviour
{
public float rotationYSpeed = 35f;
public float rotationXSpeed = 35f;
public VariableJoystick gunJoystick;
public GameObject Gun; //tank barrel
void Update()
{
//rotate turret (everything works because i don't need to lock the rotation)
float turretRotation = gunJoystick.Horizontal * rotationYSpeed;
turretRotation *= Time.deltaTime;
transform.Rotate(0f, turretRotation, 0f);
//rotate gun
float gunRotation = gunJoystick.Vertical * rotationXSpeed;
gunRotation *= Time.deltaTime;
Gun.transform.Rotate(gunRotation, 0f, 0f);
//this is the part where i tried to lock the rotation
float minRotation = -15;
float maxRotation = 15;
Vector3 currentRotation = Gun.transform.localRotation.eulerAngles;
currentRotation.x = Mathf.Clamp(currentRotation.x, minRotation, maxRotation);
Gun.transform.localRotation = Quaternion.Euler(currentRotation);
}
}
Answer by Pangamini · Sep 08, 2021 at 12:59 AM
The problem with BobyStar's solutioni is that he keeps reacquiring euler angles from the rotation. This will cause the angles to 'snap around' sometimes. For example, the same rotation can be described by many different values. -90 degrees is the same rotation as 270 degrees. This messes with your clamping. Instead, try to keep the euler angles in a local variable (Vector3 currentRotation in your case), and don't set it from Gun.transform. That way, your gun rotation will always be represented by eulerAngles, so no 'snapping around' happens (since they are just floats in a vector).
Answer by BobyStar · Sep 06, 2021 at 08:17 PM
Instead of rotating the transform, you should change currentRotation.x
by the gunRotation
then limit the rotation. Lastly set the transform's local rotation to the new currentRotation
:
// rotates currentRotation
Vector3 currentRotation = Gun.transform.localEulerAngles; // get local euler angles
float gunRotation = gunJoystick.Vertical * rotationXSpeed;
gunRotation *= Time.deltaTime;
currentRotation.x += gunRotation; // adds gunRotation to the x axis of the currentRotation
// limits currentRotation.x and applies it to the transform
float minRotation = -15;
float maxRotation = 15;
currentRotation.x = Mathf.Clamp(currentRotation.x, minRotation, maxRotation);
Gun.transform.localEulerAngles = currentRotation; // you can just use transform.localEulerAngles
unfortunately, it didn't work for me. it only restricts the downward movement, and when I try to raise the muzzle up, it resets to -15 again
Your answer
Follow this Question
Related Questions
limit rotation to 90 degree on one axis 3 Answers
How can I set a limit to my object rotation???(Here's the code) 2 Answers
Strange rotation pattern. 0 Answers
Resetting transform's original position and rotation after using transform.rotateAround 1 Answer
How can I rotate an object without moving it up or down? 0 Answers