Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Following Mechanic

Discussion in 'Editor & General Support' started by moniquetmurphy93, Jun 9, 2021.

  1. moniquetmurphy93

    moniquetmurphy93

    Joined:
    Apr 22, 2021
    Posts:
    8
    Hi all,

    I'm making a mini game in 2D, where the secondary character will follow the playable character around in specific parts of the game. I successfully coded the secondary character to follow the movements of the playable character with a short delay and it works perfectly, but I can't animate the secondary character or have it flip rotation. Would anyone have some idea's around this?

    Here's my follow code below. Leader is the playable character :) thanks!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class FollowCode : MonoBehaviour
    6. {
    7.     public GameObject leader; // the game object to follow - assign in inspector
    8.     public int steps; // number of steps to stay behind - assign in inspector
    9.     private Queue<Vector3> record = new Queue<Vector3>();
    10.     private Vector3 lastRecord;
    11.     public float chickAnimator;
    12.     Animator animChick;
    13.  
    14.     // Update is called once per frame
    15.     void FixedUpdate()
    16.     {
    17.         // record position of leader
    18.         record.Enqueue(leader.transform.position);
    19.  
    20.         // remove last position from the record and use it for our own
    21.         if (record.Count > steps)
    22.         {
    23.             this.transform.position = record.Dequeue();
    24.         }
    25.     }
    26. }
    27.  
    28.  
    29.  
     
  2. moniquetmurphy93

    moniquetmurphy93

    Joined:
    Apr 22, 2021
    Posts:
    8
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    People tend not to respond when they see you have made no visible effort.

    Your question implies that you ARE animating and flipping the main character.

    Have you tried applying the same approach you did for the main character to this follower character? I certainly don't see it in the code above! How did it go? Did it work?

    If you don't have EITHER character animated and flipping, then the answer becomes:

    "Try some Youtube tutorials about animating and/or flipping characters."

    Nobody is going to type a string of letters and numbers here to make it happen. Animation and flipping will require at least 50% of the work be done in the editor itself, and the tutorial can show you exactly how.