• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by pepperedereppep · Mar 14, 2011 at 03:54 AM · 2d

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Ipsquiggle · Mar 14, 2011 at 06:09 AM

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image pepperedereppep · Mar 14, 2011 at 08:04 AM 0
Share

Thanks for the response! I'm trying the Clamp$$anonymous$$agnitude 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..

avatar image Ipsquiggle · Mar 14, 2011 at 06:35 PM 0
Share

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

avatar image Ipsquiggle · Mar 14, 2011 at 06:37 PM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

2D Animation does not start 1 Answer

Assets/Scripts/PlayerController.cs(32,49): error CS0126: An object of a type convertible to `float' is required for the return statement 1 Answer

2D shooter, 3d or 2d colliders? 2 Answers

2D Gameplay Tutorial - Broken 1 Answer

Object positions in Scene view do not match Game view 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges