- Home /
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 :)
Answer by robertbu · Aug 07, 2014 at 08:10 PM
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; } }
Thank you for your reply! It gives an error: (17, 38) cs0117 'camera does not contain a definition for $$anonymous$$ain'
fixed the error, now i think i have everything set correctly but the pupils still wont move! they are separate to the rest of the body but still parented to the armature (so that the eyes and face can move with one bone), i'm wandering if parenting it to the armature has caused this?
@RosieSpud$$anonymous$$ole To fix this error, in the last line you need to change "center" by "reference.transform.position".
The parenting to the armature is a possibility, but I'm not sure. As a test do:
Start a new scene
Drop in a Quad with a circle (or even use a sphere)
Add the script
Hit pay and adjust settings as necessary
If this works, you know that it is something in the way you setup your eyes. If it does not work, then you have some setting that is different than what I used to test the code.
Restating, this code assumes an Orthographic camera and the object as a 'z' = 0.0 position.
Answer by mastercarl · Jan 28, 2018 at 07:16 AM
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;
}
Answer by Chintan-Kansagara · May 03, 2018 at 10:28 AM
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,
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Camera move when reach edge of screen 3 Answers
A node in a childnode? 1 Answer
3D Gravity towards one object 3 Answers
Distribute terrain in zones 3 Answers