- Home /
teeworlds eyes animation
In the 2d multiplayer shooter game named "Teeworlds" the eyes of the character move on a circle so they always "look" at the mouse position. I want to realise this in my game too but I do not even have a hint how to do this. Could anyone give me a tip or a direction to go?
Kind Regards
Answer by pwnageman2 · Jan 14, 2015 at 08:27 AM
I would have an image for the pupil (black part) of the eye as a separate object than the white part of the eye. Then in a script attached to the pupil, do something like the following:
1- Store the initial position of the pupil (start the pupil in the center of the eye in the scene):
// before Start () of pupil script
Vector3 initialPos;
// in Start () of pupil script
initialPos = transform.position;
2- Find the direction from the pupil to the mouse by doing something like this:
// in Update() of pupil script
Vector2 mousePosition = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
Vector2 pupilPosition = new Vector2(transform.position.x, transform.position.y);
Vector2 directionToMouse = mousePosition - pupilPosition;
3- Clamp this direction Vector so that the pupil won't go outside the radius of the eye:
// still in Update ()
float radius = 1f; // put in your eye's radius here. you can try various values.
Vector2 posToAdd = Vector2.ClampMagnitude (directionToMouse, radius);
4- Add this calculated vector to the initial position of the pupil object:
// still in Update()
transform.position = initialPos + new Vector3 (posToAdd.x, posToAdd.y, 0f);
That's it! I haven't tested it, but you should be able to fix any syntax errors as long as you understand what is happening.
Thank you, this helped alot! The only thing I had to change was the transform.position to transform.localPosition, otherwise the eyes not move with the character.
Awesome! Please upvote and accept this answer if it helped you
Answer by Berenger · Jan 14, 2015 at 08:21 AM
Here are some stuff you'll need :
Input.mousePosition, camera.ScreenToWorldPoint, transform.LookAt.
I suggest you parent the eyes to a gameobject at the center of the rotation you want, and manipulate that guy instead of the eyes directly. Keep in mind that there is always many ways to do something, and the best one usually is the one you understand the most.
Your answer
![](https://koobas.hobune.stream/wayback/20220613180325im_/https://answers.unity.com/themes/thub/images/avi.jpg)