How to make an object follow my player ? (with rotation facing my player)

Hi ,

I’m making a 2D game and want a 2D object follow my player and facing to my player also (rotates).
Please help ASAP , I’ll be really thankful !

Appreciate every response :slight_smile:
Thanks.

1 Like

I just gave a snippet to another user to make an object look at the mouse, so I adapted it to make it look at a Transform provided in the inspector, and also to move towards that same Transform’s position over time.

Let me know if this works for you, and if you need further explanation.

Be sure to set the variables in the inspector when you try it.

using UnityEngine;

public class FollowTarget : MonoBehaviour
{
    [Tooltip("Target to follow.")]
    public Transform target;

    [Tooltip("Optional offset from the target's position to move towards.")]
    public Vector3 followOffset;

    [Tooltip("Speed to move towards the target.")]
    public float followSpeed;

    [Tooltip("True if this object should rotate to face the target.")]
    public bool lookAtTarget;

    [Tooltip("Speed to rotate towards the target.")]
    public float lookSpeed;

    // done in LateUpdate to allow the target to have the chance to move first in Update
    private void LateUpdate()
    {
        // move towards the target position ( plus the offset ), never moving farther than "followSpeed" in one frame.
        transform.position = Vector3.MoveTowards(transform.position, target.position + followOffset, followSpeed);

        if(lookAtTarget)
        {
            // get a rotation that points Z axis forward, and the Y axis towards the target
            Quaternion targetRotation = Quaternion.LookRotation(Vector3.forward, (target.position - transform.position));

            // rotate toward the target rotation, never rotating farther than "lookSpeed" in one frame.
            transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, lookSpeed);

            // rotate 90 degrees around the Z axis to point X axis instead of Y
            transform.Rotate(0, 0, 90);
        }
    }
}

THANKS A LOT ! I just had to replace the transform.Rotate() from (0,0,90) to (0,0,1) because i want it to be smooth and not to rotate 90 degrees each time…
Anyhow everything else worked Incredibly awesome thanks again , really appreciate it :slight_smile:

2 Likes

Just to let you know, that “transform.Rotate()” was not affecting the smooth motion. It was making the object face the target with the X axis instead of the Y axis.

You can remove that line altogether if it is working for you. No need to rotate (0,0,1).

Just for the record, I expanded on the script a little bit more to provide the functionality to follow until within a certain radius of the target ( plus any offset), and a boolean for using Y axis to face the target.

using UnityEngine;

public class FollowTarget : MonoBehaviour
{
    [Tooltip("Target to follow.")]
    public Transform target;

    [Tooltip("How close this object should try to get to the target plus the offset before stopping.")]
    public float followRadius;

    [Tooltip("Optional offset from the target's position to move towards.")]
    public Vector3 followOffset;

    [Tooltip("Speed to move towards the target.")]
    public float followSpeed;

    [Tooltip("True if this object should rotate to face the target.")]
    public bool lookAtTarget;

    [Tooltip("Speed to rotate towards the target.")]
    public float lookSpeed;

    [Tooltip("True if the Y Axis should be used as the 2D forward axis. (Defaults to X Axis)")]
    public bool lookWithY;

    // done in LateUpdate to allow the target to have the chance to move first in Update
    private void LateUpdate()
    {
        Vector3 targetPosition = target.position + followOffset;
        if(Vector3.Distance(transform.position, targetPosition) > followRadius)
        {
            // move towards the target position ( plus the offset ), never moving farther than "followSpeed" in one frame.
            transform.position = Vector3.MoveTowards(transform.position, targetPosition, followSpeed);
        }

        if(lookAtTarget)
        {
            // get a rotation that points Z axis forward, and the Y axis towards the target
            Quaternion targetRotation = Quaternion.LookRotation(Vector3.forward, targetPosition - transform.position);

            // rotate toward the target rotation, never rotating farther than "lookSpeed" in one frame.
            transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, lookSpeed);

            // rotate 90 degrees around the Z axis to point X axis instead of Y
            if(!lookWithY)
            {
                transform.Rotate(0, 0, 90);
            }
        }
    }
}

Thanks man :slight_smile:

1 Like

Thank you, Brazil here '. :slight_smile:

Thanks for the post, Brazil here '. :slight_smile: