How to make eyes follow player?

I have rigged cartoon looking character. In the eyes there is one bone for each eye. Pivot point is in the middle of the eyeball and the iris moves when I rotate the bone.

Is it possible to make a script (I write with javascript) that would move the “eye-bones” towards the player? That way it would look like the character is following player with his eyes.

Yes, bones are just transforms, so you can use a lookat with them. You have to use LateUpdate incase there are any animations connected to them.

Yeah, just use LookAt as fire7side said :

var Player : Transform;
function Update(){
transform.LookAt(Player);
}
1 Like

Thank you for the help!

I tried that but the eye bone seems to direct to the right. And it only seems to rotate in one axis when walk “under the eye”. It only moves sideways.

I made a box that shows the bone that I’m trying to get to face the player.

Here’s a short video of the eyeball.

If you used the code that Intense_Gamer94 posted, make sure your eyes’ forward direction is the z-axis in Unity.

That will hopefully solve the problem.

Yes, forward depends on the modeler you used. You’ll have to work that out somehow.

I finally got it working. Like you said the problem was with the forward direction.

Here’s the result with the game-character called Kispi. He has 8 eyes in total. :slight_smile:

Next I quess I have to make a trigger that makes the eyes follow only when player is in front of the character. Since now the iris moves un-naturaly 360 degrees. But this was a big step forward for me and this should work when animated.

Thanks, everyone!

1 Like

Yeah, and make them blink once in a while. Of course, my character doesn’t blink right now but it’s not quite so obvious.

I didn’t even think of blinking. All these “small things” like eyes following and blinking makes the illusion of lifelikeness of the character more effect. And the eyes are important anyway because you look at them. I will work on that next.

I quess one way of doing the blinking is to change the texture of eyeball to skincolor (and hide the rendering of iris) for a second once in a while.

Now the eyes blink and it feels better than before.

Since this is thread about scripting eyes here’s the code I wrote. I don’t know if there is any use for it but here it is anyway.

#pragma strict

var eyeOpen 					: Texture;
var eyeClosed 					: Texture;
var iris						: Transform;
var iris2						: Transform;
var secondEyeball				: Transform;
private var nextBlink			= 0.0;

function Update () 

{

		if (Time.time > nextBlink) 
		{
			nextBlink = Time.time + Random.Range(2.0, 12.0);
			blink();
		}

}

function blink ()

{

	iris.GetComponent(SkinnedMeshRenderer).enabled = false;
	iris2.GetComponent(SkinnedMeshRenderer).enabled = false;	

    renderer.material.mainTexture = eyeClosed;	
	secondEyeball.GetComponent(Renderer).material.mainTexture = eyeClosed;
	
	yield WaitForSeconds(Random.Range(0.10, 0.30));	

	iris.GetComponent(SkinnedMeshRenderer).enabled = true;		
	iris2.GetComponent(SkinnedMeshRenderer).enabled = true;		

    renderer.material.mainTexture = eyeOpen;
	secondEyeball.GetComponent(Renderer).material.mainTexture = eyeOpen;

}
2 Likes

Nice job! That looks a lot better. Interesting solution.