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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How do you make an object follow your exact movement, but delayed?

Discussion in 'Scripting' started by Wantcha, Jan 15, 2018.

  1. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    Wow, such an old post. I'm glad it help you out!
     
  2. Reaper_202307

    Reaper_202307

    Joined:
    Jul 28, 2019
    Posts:
    1
    This helped me out a ton with my game. Quick question though, I'm making a 2d game like deltarune, and I'm trying to make a party follow system similar to what that has. It's top down, with pixel sprites and 8 directional movement, and I have code for the animations almost done, but the characters that are following in the party keep playing the walking animations after they stop, and I don't know how to fix it.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class FollowTheLeader : MonoBehaviour
    6. {
    7.     public GameObject player;
    8.     public int followDistance;
    9.     private List<Vector3> storedPositions;
    10.  
    11.     public Animator thisAnimator;
    12.     public Animator mcAnimator;
    13.     public List<float> horAnimVectors;
    14.     public List<float> vertAnimVectors;
    15.     public List<float> spdAnimVectors;
    16.     public List<float> spdmAnimVectors;
    17.  
    18.  
    19.     void Awake()
    20.     {
    21.         storedPositions = new List<Vector3>();
    22.         horAnimVectors = new List<float>();
    23.         vertAnimVectors = new List<float>();
    24.         spdAnimVectors = new List<float>();
    25.         spdmAnimVectors = new List<float>();
    26.     }
    27.  
    28.     void Start()
    29.     {
    30.  
    31.     }
    32.  
    33.     void Update()
    34.     {
    35.         if (storedPositions.Count == 0)
    36.         {
    37.             storedPositions.Add(player.transform.position);
    38.             horAnimVectors.Add(mcAnimator.GetFloat("Horizontal"));
    39.             vertAnimVectors.Add(mcAnimator.GetFloat("Vertical"));
    40.             spdAnimVectors.Add(mcAnimator.GetFloat("Speed"));
    41.             spdmAnimVectors.Add(mcAnimator.GetFloat("SpeedMultiplier"));
    42.             return;
    43.         }
    44.         else if (storedPositions[storedPositions.Count - 1] != player.transform.position)
    45.         {
    46.             storedPositions.Add(player.transform.position);
    47.             horAnimVectors.Add(mcAnimator.GetFloat("Horizontal"));
    48.             vertAnimVectors.Add(mcAnimator.GetFloat("Vertical"));
    49.             spdAnimVectors.Add(mcAnimator.GetFloat("Speed"));
    50.             spdmAnimVectors.Add(mcAnimator.GetFloat("SpeedMultiplier"));
    51.         }
    52.  
    53.         if (storedPositions.Count > followDistance)
    54.         {
    55.             transform.position = storedPositions[0];
    56.             storedPositions.RemoveAt(0);
    57.  
    58.             thisAnimator.SetFloat("Horizontal", horAnimVectors[0]);
    59.             thisAnimator.SetFloat("Vertical", vertAnimVectors[0]);
    60.             thisAnimator.SetFloat("Speed", spdAnimVectors[0]);
    61.             thisAnimator.SetFloat("SpeedMultiplier", spdmAnimVectors[0]);
    62.             horAnimVectors.RemoveAt(0);
    63.             vertAnimVectors.RemoveAt(0);
    64.             spdAnimVectors.RemoveAt(0);
    65.             spdmAnimVectors.RemoveAt(0);
    66.         }
    67.     }
    68. }
    the "spd" and "spdm" floats are for speed and speed multiplier, for when the characters are moving and running.

    I assume I need to do something with the "speed" float and change it in a different way than the other floats, but I'm not quite sure how? Any advice?
     
  3. girguy101

    girguy101

    Joined:
    Dec 11, 2022
    Posts:
    3
    ok so I'm trying to use this thing called Mai Follower https://pinkbunny.tech/mai-follower/ which should be plug and play, but ever since dynamic bones changed to physbones it not longer works. It only works if you remove the HorizonDelay and VerticalDelay classes. the problem with those classes is that they lack a script. these two classes are supposed to emulate 'hovering' delay on the X and Y axes, to lag behind the player as they move. what script can I put in these two classes so I'm not getting compiler errors? thanks
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,814
    Please don't necro post. If you have a problem start your own post... it's FREE!

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/