Lookat, one axis, with contraint

I’m trying to have gun rotate to point at the mouse, but only on one axis,
and that axis must be within a set range. Basicaly what a I thought a configurable
joint would do(it doesnt seem to affect it though). I would also like to sendmessage
when the mouse enters or exits that range, but that isn’t very important.

I have searched around and I’m at a total loss.
I’m happy with any method as long as it works.

Thanks in advance

The Unity Transform.LookAt method has an override that allows passing in a World Position Vector3:

Something along the lines of:

using UnityEngine;
using System.Collections;

public class LookAt : MonoBehaviour {

    Vector3 lookAtPoint = new Vector3();
	// Update is called once per frame
	void Update () {
        if (Input.GetMouseButton(0))
            Ray ray = Camera.mainCamera.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit, Mathf.Infinity))
                lookAtPoint.x = hit.point.x;
                lookAtPoint.y = 0;
                lookAtPoint.z = hit.point.z;

I haven’t tested this, but it should work. This is assuming you’re ray casting the mouse position against a collider, like some ground. You can’t really transform the mouse position into world space for your gun to look at unless you have a reference object for the mouse position to convert to.

As far as not ray casting every frame for the mouse to check against, I’ll let you come up with your own solution for that.