You can use just one segment of code from robertbu's answer. Here:
var toTarget : Quaternion = Quaternion.LookRotation(target.position - turret.position, turret.up);
transform.rotation = Quaternion.RotateTowards(transform.rotation, toTarget, turretSpeed * Time.deltaTime);
//freeze Z-axis to stop turret from rolling
transform.localRotation.z = Quaternion.identity.z;
With these alone, the turret will rotate towards the target on all axes, except Z.
Use the ClampAngle function specified in aldonaletto's answer here:
[Limit Local Rotation][1]
It automatically converts negative values into usable eulers. Of course, you'll need minimum and maximum values for your X and Y axes. Use it like this:
var angleX = ClampAngle(turret.localEulerAngles.y,minX,maxX);
var angleY = ClampAngle(turret.localEulerAngles.x,minY,maxY);
turret.localEulerAngles = new Vector3(angleY,angleX,turret.localEulerAngles.z);
[1]: http://answers.unity3d.com/questions/141775/limit-local-rotation.html
↧