The goal is to clamp the turret transform so that it can only rotate around the Y axis, providing for horizontal-only turret rotation.
First, my code so far:
var turret: Transform;
var barrel: Transform;
function Update () {
// Get the Vector3 of where the camera is looking
var aimpoint : Vector3 = camera.ViewportToWorldPoint(Vector3(.5,.5,250));
// Clamp the turret's rotation
// ????
// Get the raw rotation and feed it to the turret
turret.transform.rotation = Quaternion.FromToRotation(Vector3.forward,aimpoint);
}
In this circumstance, the script is being run from a camera which orbits the turret. The camera is also a child of the turret.
What I am trying to do is cause the turret to transform in such a manner as to point at the same location as is given by the camera.ViewportToWorldPoint function passed from the center of the camera.
The script works a little too well as the turret transform rotates to face the aimpoint on all axes.
I have been looking for a way to clamp the transform so that it can only pivot on the Y axis (horizontal rotation only), however I’m having no success at it. I have tried various combinations of Vector3 and Mathf.Clamp but can’t seen to make it work.
It has also occurred to me that I may be able to pass only a particular axis from camera.ViewportToWorldPoint to the variable which then gets passed to Quaternion.FromToRotation, but I have no idea how to do that.
Any help would be greatly appreciated.
NOTE: Edits are to attempt to fix the code not being displayed correctly in the final question.