Qookie Games
How to Create Eyes That Follow a Character or the Mouse Cursor
Have you ever wondered how to create an engaging and interactive eye-tracking effect in Unity 2D? This tutorial is perfect for both beginners and experienced developers looking to add a fun and dynamic element to their games. We’ll guide you through the process step-by-step, ensuring you understand each part of the script and how it contributes to the final effect.
First, create a new script named “EyesControl.” This script will handle all the logic for our eye-tracking effect. Inside the script, define a GameObject called “Eyes” and a camera variable to reference the main camera.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EyesControl : MonoBehaviour
{
public GameObject eyes;
public Camera camera;
public Transform target;
public float speed = 10;
public float intensity = 0.3f;
void Start()
{
camera = Camera.main;
}
// Update is called once per frame
void Update()
{
if (camera != null)
{
EyesAim();
}
// if (target != null)
// {
// EyesAim2();
// }
}
void EyesAim()
{
/* Get the mouse position in world space rather than screen space. */
var mouseWorldCoord = camera.ScreenPointToRay(Input.mousePosition).origin;
/* Get a vector pointing from initialPosition to the target. Vector shouldn't be longer than maxDistance. */
var originToMouse = mouseWorldCoord - this.transform.position;
originToMouse = Vector3.ClampMagnitude(originToMouse, intensity);
/* Linearly interpolate from current position to mouse's position. */
eyes.transform.position = Vector3.Lerp(eyes.transform.position, this.transform.position + originToMouse, speed * Time.deltaTime);
}
void EyesAim2()
{
/* Get a vector pointing from initialPosition to the target. Vector shouldn't be longer than maxDistance. */
var originToTarget = target.position - this.transform.position;
originToTarget = Vector3.ClampMagnitude(originToTarget, intensity);
/* Linearly interpolate from current position to mouse's position. */
eyes.transform.position = Vector3.Lerp(eyes.transform.position, this.transform.position + originToTarget, speed * Time.deltaTime);
}
}
Adding Sprites
Next, let’s add some sprites to test the code. You can download sprites from Kenney’s assets or any other sprite library. Add the eyes as a child object of your main GameObject and attach the “EyesControl” script to the parent GameObject. Ensure you assign the eyes GameObject to the script in the Inspector.
Customizing the Effect
To control how much the eyes move, we added an intensity variable. You can adjust this to increase or decrease the responsiveness of the eyes. Additionally, by adding a speed variable, you can control the speed of the eye-tracking movement, making it smoother or more abrupt based on your game’s needs.
Testing and Duplicating
Now, test your setup by running the game. The eyes should follow the mouse cursor or the assigned GameObject. You can duplicate the GameObjects, and all of them will independently follow the cursor or target.
Conclusion
With these steps, you’ve created a simple yet effective eye-tracking effect in Unity 2D. This effect can add a lot of character and interactivity to your game, making it more engaging for players.