- Home /
Turret Limitations on a Moving Object
I'm attempting to create a turret that follows it's parent spaceship, rotates towards the mouse, and has limitations on where it can rotate to. The problem occurs when attempting to clamp the turret to it's limitations. The clamp refuses to go beyond a certain point.
The circle is where the turret starts off, the blue lines represent the rotation limits that happen at the two points. The original point works perfectly, and rotates towards the mouse inside of the clamping region. However, if I rotate the ship by say 180 degrees, and the turret ends up at the top point, it gets stuck and goes back to it's original clamp position when it reaches half way. The rotation in the inspector claims that it only goes up to 180 before resetting.
Here is my code for the turret:
public float speed;
public Vector3 rotate;
public GameObject parent;
Vector3 pos;
void Update(){
Vector3 mousePos = Input.mousePosition;
pos = Camera.main.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, Camera.main.transform.position.y - transform.position.y));
}
void FixedUpdate(){
PlayerMove p = parent.GetComponent<PlayerMove>();
Quaternion targetRot = Quaternion.LookRotation(pos - new Vector3(transform.position.x,0,transform.position.z));
Vector3 rotate = Vector3.up * Mathf.MoveTowardsAngle(transform.eulerAngles.y, targetRot.eulerAngles.y, speed * Time.deltaTime);
rotate.y = Mathf.Clamp(rotate.y, p.currentDir + 90, p.currentDir + 270);
transform.eulerAngles = rotate;
}
Using localRotation causes it to spaz out at any angle other than the original. I have been tampering with this for multiple hours now, and have managed to pull of my hair out, had to resort to asking for help before tearing skin apart as well.
Any and all help will be greatly appreciated.
EDIT: I have now identified the problem, after x amount of time passing, I've learnt that the problem happens whenever the angle goes past zero. For some odd reason, the turret refuse to go through zero without going the other way, causing it to go back to it's other limit. While I thought that the Vector3 just has problems with going through zero (which happened in another project) I attempted to use a different means of rotation which worked on the other project, here is the new code:
public float speed;
public float min;
public float max;
public Vector3 rotate;
public PlayerMove p;
public Quaternion targetRot;
Vector3 pos;
float r;
void Start(){
p = GetComponentInParent<PlayerMove>();
}
void Update(){
Vector3 mousePos = Input.mousePosition;
pos = Camera.main.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, Camera.main.transform.position.y - transform.position.y));
min = p.currentDir + 90;
max = p.currentDir + 270;
}
void FixedUpdate(){
targetRot = Quaternion.LookRotation(pos - new Vector3(transform.position.x,0,transform.position.z));
r = targetRot.eulerAngles.y;
r = Mathf.Clamp(r, min, max);
transform.rotation = Quaternion.Euler(0, r, 0);
//rotate = Vector3.up * Mathf.MoveTowardsAngle(transform.eulerAngles.y, r, speed * Time.deltaTime);
//transform.eulerAngles = rotate;
}
I used Quaternion.Euler with the individual variables rather than the Vector3, as this seemed to work the last time I encountered this problem.
EDIT2: I've learned that it isn't just when going through zero, it's whenever the min or max end up in the negatives as the ship continuously rotates. The clamp borrows the variables from the ship rotation, which is a float that increases and decreases as the ship rotates. There isn't a clamp on that, since that restricts the ship rotation.
Still hoping for an answer...
I've had problems with this as well and never quite fixed it. If the turret only has to move along 1 axis, freeze the others on the rigidbody. Then, in the late update, you could try adding an if-statement that only allows movement if the rotate.y is between certain angles.
@Ricewind | It doesn't use the Rigidbody in any of the objects, and the rotation is only changed along the y axis. Before the turret starts moving, the possible angles are clamped to the limits, meaning that the turret was never able to rotate past those angles before movement started. It works fine in the original angle, just not at any other angle.
Your answer
![](https://koobas.hobune.stream/wayback/20220613200729im_/https://answers.unity.com/themes/thub/images/avi.jpg)