Forum rules - please read before posting.

Eyes follow character around scene

Hi,

Has anyone tried to implement or know how to get objects to follow the character around a scene? I have a number of eerie dolls and I want their eyes to follow the character around the scene, anyone tried to do this?

Comments

  • A custom script can read the position of the Player with:

    AC.KickStarter.player.transform.position
    

    And update a sprite or transform accordingly.

  • You'll need custom scripting to do this. The way you do it also depends on if you're talking 3D eyes, or textures...etc

    I used this as a guide for doing it in my game: https://www.noveltech.dev/unity-implement-eye-object-tracking-2/

  • Ok awesome I’ll check it put 2d game so just sprites of eyes
  • I’ll try put your tutorial and see if I can get it working!
  • so this script works perfectly, but, only if player is a gameobject in the scene, I want my player to be prefab from settings manager Character settings, so how do I tweak this script to pull from there instead of game object in scene?

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Eye : MonoBehaviour {

    public Transform Pupil;
    public Transform Player;
    public float EyeRadius = 1f;
    Vector3 mPupilCenterPos;
    
    void Start()
    {
        mPupilCenterPos = Pupil.position;
    }
    
    void Update()
    {
        Vector3 lookDir = (Player.position - mPupilCenterPos);
        if (lookDir.magnitude > EyeRadius)
            lookDir = lookDir.normalized * EyeRadius;
    
        Pupil.position = mPupilCenterPos + lookDir;
    }
    

    }

  • using UnityEngine;
    
    public class Eye : MonoBehaviour
    {
    
        public Transform Pupil;
        public float EyeRadius = 1f;
        Vector3 mPupilCenterPos;
    
        void Start ()
        {
            mPupilCenterPos = Pupil.position;
        }
    
        void Update ()
        {
            Vector3 lookDir = (AC.KickStarter.player.transform.position - mPupilCenterPos);
            if (lookDir.magnitude > EyeRadius)
                lookDir = lookDir.normalized * EyeRadius;
    
            Pupil.position = mPupilCenterPos + lookDir;
        }
    
    }
    
  • Amazing thank you!

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.