Have an object follow mouse cursor in 3d space, like an Eyeball looking at the mouse

Hi everyone, I’m new at this stuff but am learning quickly. Basically, I have a (sphere) Eyeball in my scene. All I want to do, is have the eyeball track or look at the mouse when i wave it around the space. I realize i need to use 2d mouse movements with a 3d eyeball, but not sure where to begin. My goal is to have, on an iphone, the user drag their finger around the eyeball, and have the eye point wherever the user’s finger is, simple enough.

I’ve used this script, but when i go vertically, the eye spins on the wrong axis, and never looks up or down. Thanks!

var position = Input.mousePosition;
    newposition = Vector3(position.x,position.y,-camera.main.transform.position.z);
    var lastposition = camera.main.ScreenToWorldPoint(newposition);
    transform.LookAt(lastposition);
}

I just played around with this for a bit. Couldn’t get it to work well with Camera.ScreenToWorldPoint(), so tried Camera.ScreenPointToRay() instead with much better results.

Something like this:

	var depth = 10;
	var pos = Camera.main.ScreenPointToRay(Input.mousePosition).GetPoint(depth);
	transform.LookAt(pos);

My “EyeBall” code. Look At Mouse :slight_smile:

public class CustomController : MonoBehaviour {
	
	GameObject eye;
	
	void Start () {
		this.eye = GameObject.Find("MyEye");
	}
	
	void Update () {
		Vector3 pos = Camera.main.ScreenPointToRay(Input.mousePosition).GetPoint(20);
		Vector3 invertedMousePos = new Vector3(-pos.x * (float)0.2, -pos.y * (float)0.2, pos.z);
		this.eye.transform.LookAt(invertedMousePos);
	}
}