Unity: C#: Eyes look at mouse: HELP!!

This is my character, my goal is to get his eyes look as if they are looking at the cursor, I have been stuck trying to create this for two days now and have yet to find a way that works!!
Using the following code I was able move my eyes towards the cursor, however this only works around the bottom left of the screen! (If the cursor is not below the screen or to the left of the screen the eyes move to the top right)

using UnityEngine;
using System.Collections;

public class LookAtMouse : MonoBehaviour {


	public float speed = 5f;
	private Vector3 target;
	public Transform origin;

	void Start () {
		target = transform.position;


	}
	
	void Update () {

			target = (Input.mousePosition);
			target.z = transform.position.z;

		transform.position = Vector3.MoveTowards(origin.position, target, speed * Time.deltaTime);
	}   
}

The character you see above is made from separate circle planes (eye base which is blue and white, the eye pupil and the glint in the eye which is a white circle) all in one mesh. I tried to assign an armature bone to the pupils and control them using that (this worked in editor but not in game mode).
If anyone could point me in the right direction I would be incredibly grateful!
Thank you :slight_smile:

Below is a script for you. it assumes 2D Orthographic camera with the object at z = 0. There are three things to setup:

  • ‘reference’ is a game object used for the calculation. It cannot be the game object with the moving eye, and it cannot be a child of the eye, but if the face moves, it must be a child of the face. Use an empty game object, or some primitive game object that you can turn the renderer off once you’ve place the object. If you place the object 1/2 of the way between the two eyes, then the eyes will move in tandem as if they are following something at a great distance. If the object is placed at the center of the eye, then the eyes will move independently as if the finger is very close. And you can vary it somewhere in-between.
  • ‘limit’ sets how far to allow the eye to move. Adjust so that the eye only moves to the outside of socket. Values will depend on the scale of your face.
  • ‘factor’ sets how quickly the eye with respect to the finger move. Higher values for closer finger, and lower values for far away finger. As an example, in my quick test, my limit was 0.25 and for far away movement, I used 0.01 for ‘factor’.

using UnityEngine;
using System.Collections;

public class EyesLook : MonoBehaviour {

	public Transform reference;
	public float factor = 0.25f;
	public float limit = 0.08f;

	private Vector3 center;

	void Start() {
		center = transform.position;
	}
	
	void Update () {
		Vector3 pos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
		pos.z = 0.0f;
		Vector3 dir = (pos - reference.position) * factor;
		dir = Vector3.ClampMagnitude(dir, limit);
		Vector3 tt = center + dir;
		transform.position = center + dir;
	}
}

I modified @robertbu 's script to work with a 2D canvas and a Perspective camera.

public Transform reference;
public float factor = 0.25f;
public float limit = 0.08f;

private Vector3 center;
private Vector3 referencePoint;

void Start() {
	referencePoint = transform.position;
	center = RectTransformUtility.WorldToScreenPoint (null, transform.position);
}

void Update () {
	Vector3 mousePos = Input.mousePosition;
	Vector3 dir = (mousePos - center) * factor;
	dir = Vector3.ClampMagnitude(dir, limit);
	Vector3 tt = referencePoint + dir;
	transform.position = tt;
}

Hello @mastercarl

Plz help me, my problem is i am develop snake game and rotate to face in mouse direct and eye usder black part do not rotate plz help me.

  • Ref. Game Name : http://wormax.io/ Snake eye rotate same my requirement.
    plz help me
    thanks,