2d eye movement

hey.. so i'm trying to code these 2d eyes that would kinda look like this: http://s10.thisnext.com/media/largest_dimension/337F23CD.jpg

Right now I have two circle mesh, one being a flat iris and the other a flat look of an eyeball.. I want the iris to move around looking at a target object while staying within the eye circle.. Right now what I've done is group the two circles into one gameobject, I applied a mathf.lerp to have it follow the object while still clamped within the eye...But I can't get the effect i'm looking for at all and I'm wondering if someone knows of a good way to create an effect like this. Any help would be great thanks!

1 Answer

1

So what you want is for each pupil to move directly towards the target (relative to the center of the eye) but never go further than the edge of the eye.

Try something like this:

// first, find the distance from the center of the eye to the target
var distanceToTarget : Vector3 = target.transform.position - eye.transform.position;

// clamp the distance so it never exceeds the size of the eyeball
distanceToTarget = Vector3.ClampMagnitude( distanceToTarget, eyeRadius );

// place the pupil at the desired position relative to the eyeball
var finalPupilPosition : Vector3 = eye.transform.position + distanceToTarget;
pupil.transform.position = finalPupilPosition;

An advantage of this method is that it automatically accounts for the target being 'over' the eye.

If you want smooth motion, just consider `finalPupilPosition` to be the destination, and use some kind of smoothing to move the pupil to that destination.

Thanks for the response! I'm trying the ClampMagnitude but it's giving me an error. How would you write that line on csharp? also, for eyeRadius are you using just a float? because it seems to require a vector3 and a float..

Aaah, sorry, thought that method supported in-place syntax also. Edited. And yes, eyeRadius would be a float.

You may also want to do eyeRadius - pupilRadius in that position to keep the pupil entirely within the eye shape.