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);
}
}