Search Unity

Question Trying to create a followingPlayer movement script but upon build Update() issues arise.

Discussion in '2D' started by plasmaghost01, Mar 3, 2021.

  1. plasmaghost01

    plasmaghost01

    Joined:
    Mar 18, 2020
    Posts:
    1
    Hello, I'm trying to create a script which moves members of your party in a line behind you (think Chronotrigger or Earthbound)
    (skip to 5:45 for reference)

    Problem:
    Everything looks fine in the Game window when I play, but when I build the game to test, the characters begin to stack one on top of each other. I assume it has something to do with Update() frames increasing dramatically the frequency with which I enqueue the positions of the leading character (although I'm not experienced enough to say for sure).
    • I've tried increasing the offset, but this solution is variable depending on the players computer so that wont work.
    • I've tried doing everything in FixedUpdate() but a lot of my other systems run on Update() and so everything stutters..
    • I've tried enqueue'ing from an IEnumerator and waiting for .1 second between each enqueue but that stutters as well.

    What I'm doing:
    The leading character has a script called PlayerTrail which creates a Queue of Vector3's. In Update the gameObjects transform.position is enqueued. The following character has a script called PlayerFollower which looks at the Queue and Vector3.MoveTowrds the position, then dequeues that position and repeats.

    Code Supplied Bellow:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerTrail : MonoBehaviour
    6. {
    7.     public Queue<Vector3> LeaderTrail;
    8.     public Vector3 LastMovement;
    9.  
    10.     private void Start()
    11.     {
    12.         LeaderTrail = new Queue<Vector3>();
    13.     }
    14.  
    15.     private void  Update()
    16.     {
    17.         if (this.transform.position != LastMovement)
    18.         {
    19.             LastMovement = transform.position;
    20.             LeaderTrail.Enqueue(this.transform.position);
    21.         }
    22.  
    23.     }
    24. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerFollower : MonoBehaviour
    6. {
    7.     public GameObject leaderObject;
    8.     public int Offset;
    9.     public int moveSpeed;
    10.  
    11.     private PlayerTrail leaderTrail;
    12.     private Animator animator;
    13.  
    14.  
    15.     void Start()
    16.     {
    17.         animator = gameObject.GetComponent<Animator>();
    18.         leaderTrail = leaderObject.GetComponent<PlayerTrail>();
    19.     }
    20.  
    21.     void Update()
    22.     {
    23.         if (leaderTrail.LeaderTrail.Count < Offset)
    24.         {
    25.                 animator.SetBool("isMoving", false);
    26.                 return;  
    27.         }
    28.         else
    29.         {
    30.             Vector3 direction = (leaderTrail.LeaderTrail.Peek() - transform.position).normalized;
    31.             this.transform.position = Vector3.MoveTowards(this.transform.position, leaderTrail.LeaderTrail.Peek(), moveSpeed * Time.deltaTime);
    32.  
    33.             animator.SetBool("isMoving", true);
    34.             animator.SetFloat("moveX", direction.x);
    35.             animator.SetFloat("moveY", direction.y);
    36.  
    37.             Debug.DrawRay(this.transform.position, direction);
    38.  
    39.             leaderTrail.LeaderTrail.Dequeue();
    40.         }
    41.     }
    42. }
     

    Attached Files:

  2. Derekloffin

    Derekloffin

    Joined:
    Mar 14, 2018
    Posts:
    322
    Try moving your routine to the fixedupdate instead. When dealing with actual game behavior, fixedupdate is a better place to put it as it isn't frame rate dependent.

    Edit: Of course, do note you'll need to adapt for the speed of fixedupdate cycling. You can go into your setting for your project and set fixedupdates to be faster, but probably shouldn't need to.