- Home /
Problem is not reproducible or outdated
Problems with turret rotation clamp
I've got a little game I'm working on, and I've got a turret set up on another gameobject. This turret is rotated by the mouse. They are also clamped so they can only rotate so far. However, I have a slight issue. When I move the mouse around passed the clamp, they don't move, however as soon as I turn the mouse back they do again. Just that they no longer are looking at the mouse. I have a vague idea of what's going on, they are following the increase of the mouse axis but once I start to decrease it they instantly start to move, despite the mouse not being what they are facing. However, I'm a bit unsure as to how to solve this issue. Any help would be greatly appreciated.
Here's the code: (Note: I know one of the rotation axis is not used, but I may change this in the future so it is.)
using UnityEngine;
using System.Collections;
public class TurretBaseController : MonoBehaviour {
public GameObject playerPos;
public Transform projSpawn;
public GameObject projectile;
public float minY = -90f;
public float maxY = 90f;
public float sensY = 600f;
public float minX = -660f;
public float maxX = 360f;
public float sensX = 300f;
float rotationY = 0f;
float rotationX = 0f;
public float rotSmooth;
//float rotationYlim = 0f;
//float rotationXlim = 0f;
public float offSetX;
public float offSetY;
public float offSetZ;
public Quaternion rotation;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//axis the object can rotate along
rotationY += Input.GetAxis ("Mouse X") * sensY * Time.deltaTime;
rotationX += Input.GetAxis ("Mouse Y") * sensX * Time.deltaTime;
//rotation limits
rotationY = Mathf.Clamp (rotationY, minY, maxY);
rotationX = Mathf.Clamp (rotationX, minX, maxX);
//assigns the rotation
rotation = Quaternion.Euler(0, rotationY, 0);
//does the rotation
transform.rotation = Quaternion.Slerp (transform.rotation, rotation, rotSmooth);
}
}
Answer by Mikilo · May 19, 2016 at 08:42 AM
Hello,
You clamp rotation[X|Y] lines 42 and 43, as far as you are higher or lower than the max or the min, you are not gonna move. The moment you move toward the opposite direction, rotation[X|Y] will not be clamped anymore.
Maybe you should clamp at line 47 like that:
rotation = Quaternion.Euler(0, Mathf.Clamp (rotationY, minY, maxY), 0);
Indeed, remove the previous clamps.
This way, you only clamp at the point you use the value. The value is still free to be higher or lower than the clamping limits.
That works perfectly! Thank you!
There is another tiny thing, if I change it to localRotation, turning the object which the turret is on means it no longer points towards the mouse. Rather, it keeps pointing the way it was locally. I'd imagine this is something to do with the way I've handled it actually rotating towards the mouse, but I'm not exactly sure how I'd fix it.
This is all the changes I've made to the code:
//rotation limits
//rotationY = $$anonymous$$athf.Clamp (rotationY, $$anonymous$$Y, maxY);
//rotationX = $$anonymous$$athf.Clamp (rotationX, $$anonymous$$X, maxX);
//assigns the rotation
//rotation = Quaternion.Euler(0, rotationY, 0);
rotation = Quaternion.Euler(0, $$anonymous$$athf.Clamp (rotationY, $$anonymous$$Y, maxY), 0);
//does the rotation
transform.localRotation = Quaternion.Slerp (transform.localRotation, rotation, rotSmooth);
Nice! =D
But where is your question?
If you update localRotation, yes it is local. If it worked with rotation, why changing to localRotation?
Since it's a bit hard to describe, I created a quick little video showing the problem, hopefully it will be more clear what the problem is.
Follow this Question
Related Questions
Clamping a wrapping rotation. 6 Answers
Clamp issue 1 Answer
Turrets acting oddly when clamped 1 Answer
How to use mathf.clamp? 2 Answers
Turret rotation clamp. 2 Answers