Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 RosieSpudMole · Aug 07, 2014 at 06:53 PM · c#gamemovelookat

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

alt text

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 :)

untitled.jpg (80.9 kB)
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

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;
         }
     }
    
      
    
    
    
    
    
Comment
Add comment · Show 4 · 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 RosieSpudMole · Aug 07, 2014 at 09:04 PM 0
Share

Thank you for your reply! It gives an error: (17, 38) cs0117 'camera does not contain a definition for $$anonymous$$ain'

avatar image RosieSpudMole · Aug 07, 2014 at 09:18 PM 0
Share

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?

avatar image rzjorge21 RosieSpudMole · Mar 23, 2019 at 10:14 PM 0
Share

@RosieSpud$$anonymous$$ole To fix this error, in the last line you need to change "center" by "reference.transform.position".

avatar image robertbu · Aug 07, 2014 at 10:27 PM 0
Share

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.

avatar image
0

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;
 }
Comment
Add comment · 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
0

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,

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


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