Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

Discussion in '2D' started by Loaiq1107, May 6, 2016.

  1. Loaiq1107

    Loaiq1107

    Joined:
    Aug 25, 2014
    Posts:
    48
    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 :)
    Thanks.
     
    alexjhones286 likes this.
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    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.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class FollowTarget : MonoBehaviour
    4. {
    5.     [Tooltip("Target to follow.")]
    6.     public Transform target;
    7.  
    8.     [Tooltip("Optional offset from the target's position to move towards.")]
    9.     public Vector3 followOffset;
    10.  
    11.     [Tooltip("Speed to move towards the target.")]
    12.     public float followSpeed;
    13.  
    14.     [Tooltip("True if this object should rotate to face the target.")]
    15.     public bool lookAtTarget;
    16.  
    17.     [Tooltip("Speed to rotate towards the target.")]
    18.     public float lookSpeed;
    19.  
    20.     // done in LateUpdate to allow the target to have the chance to move first in Update
    21.     private void LateUpdate()
    22.     {
    23.         // move towards the target position ( plus the offset ), never moving farther than "followSpeed" in one frame.
    24.         transform.position = Vector3.MoveTowards(transform.position, target.position + followOffset, followSpeed);
    25.  
    26.         if(lookAtTarget)
    27.         {
    28.             // get a rotation that points Z axis forward, and the Y axis towards the target
    29.             Quaternion targetRotation = Quaternion.LookRotation(Vector3.forward, (target.position - transform.position));
    30.  
    31.             // rotate toward the target rotation, never rotating farther than "lookSpeed" in one frame.
    32.             transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, lookSpeed);
    33.  
    34.             // rotate 90 degrees around the Z axis to point X axis instead of Y
    35.             transform.Rotate(0, 0, 90);
    36.         }
    37.     }
    38. }
     
    Last edited: May 6, 2016
  3. Loaiq1107

    Loaiq1107

    Joined:
    Aug 25, 2014
    Posts:
    48


    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 :)
     
    alexjhones286 and LiterallyJeff like this.
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    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).
     
    Last edited: May 6, 2016
  5. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    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.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class FollowTarget : MonoBehaviour
    4. {
    5.     [Tooltip("Target to follow.")]
    6.     public Transform target;
    7.  
    8.     [Tooltip("How close this object should try to get to the target plus the offset before stopping.")]
    9.     public float followRadius;
    10.  
    11.     [Tooltip("Optional offset from the target's position to move towards.")]
    12.     public Vector3 followOffset;
    13.  
    14.     [Tooltip("Speed to move towards the target.")]
    15.     public float followSpeed;
    16.  
    17.     [Tooltip("True if this object should rotate to face the target.")]
    18.     public bool lookAtTarget;
    19.  
    20.     [Tooltip("Speed to rotate towards the target.")]
    21.     public float lookSpeed;
    22.  
    23.     [Tooltip("True if the Y Axis should be used as the 2D forward axis. (Defaults to X Axis)")]
    24.     public bool lookWithY;
    25.  
    26.     // done in LateUpdate to allow the target to have the chance to move first in Update
    27.     private void LateUpdate()
    28.     {
    29.         Vector3 targetPosition = target.position + followOffset;
    30.         if(Vector3.Distance(transform.position, targetPosition) > followRadius)
    31.         {
    32.             // move towards the target position ( plus the offset ), never moving farther than "followSpeed" in one frame.
    33.             transform.position = Vector3.MoveTowards(transform.position, targetPosition, followSpeed);
    34.         }
    35.  
    36.         if(lookAtTarget)
    37.         {
    38.             // get a rotation that points Z axis forward, and the Y axis towards the target
    39.             Quaternion targetRotation = Quaternion.LookRotation(Vector3.forward, targetPosition - transform.position);
    40.  
    41.             // rotate toward the target rotation, never rotating farther than "lookSpeed" in one frame.
    42.             transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, lookSpeed);
    43.  
    44.             // rotate 90 degrees around the Z axis to point X axis instead of Y
    45.             if(!lookWithY)
    46.             {
    47.                 transform.Rotate(0, 0, 90);
    48.             }
    49.         }
    50.     }
    51. }
     
  6. Loaiq1107

    Loaiq1107

    Joined:
    Aug 25, 2014
    Posts:
    48
    Thanks man :)
     
    LiterallyJeff likes this.
  7. alexjhones286

    alexjhones286

    Joined:
    Mar 28, 2016
    Posts:
    5
    Thank you, Brazil here '. :)
     
  8. alexjhones286

    alexjhones286

    Joined:
    Mar 28, 2016
    Posts:
    5
    Thanks for the post, Brazil here '. :)