GameObject Trail Following Player

Hey guys!

I am currently working on adding a trail of game objects to my player who can be launched; they are animated sprites. I know I could use a particle system or something else like that - but for this project we have decided it must be game objects since it is all hand animated for the overall effect that we are going for.

So basically, I track the players velocity. If it hits a certain threshold I instantiate trail objects. The PROBLEM is if I travel a shorter distance - the spacing between objects is perfect… if I travel a longer distance I end up with large gaps in between my game objects. I am trying to have the spacing look the same regardless of the distance traveled.

This is pretty much all I am doing so far:

if(playerRbody.velocity.magnitude > 20)
       {
           CreateTrail();
       }
public void CreateTrail()
{

GameObject _trail = Instantiate(trail_prefab, player.transform.position, new Quaternion(Random.Range(0, 360),Random.Range(0, 360),0,0));

}

Here is a visual representation of what is happening. The last image is what I am trying to achieve.

Thanks guys!

If you are traveling at a greater speed then the distance traveled between each update is naturally going to be greater. So instead of spawning an object each time, record the position each update. then spawn objects in between the last position and the new position based on your separation.

Something like this(its a hack)

Vector3 lastUpdate;
float delta = 10;

void Update()
{
    var pos = transform.position;
    if (Vector3.Distance(lastUpdate, pos) > delta)
    {
        // Spawn points between
        lastUpdate = pos;
    }
}
1 Like

Okay awesome! I was trying something similar earlier, tracking the player then seeing if the player had moved the equal distance of the size of the game object sprites - but was not able to get it to run properly! I will try this now! Thank you very much!

1 Like

So pretty much this? I’m sure I’m missing something super simple, but right now - nothing is spawning.

Vector3 lastUpdate;
float delta = 10f;

void Update()
{
var pos = player.transform.position;
       if(Vector3.Distance(lastUpdate, pos) > delta)
       {
           GameObject _trail = Instantiate(trail_prefab, pos, new Quaternion(Random.Range(0, 360),Random.Range(0, 360),0,0));
           lastUpdate = pos;
       }
}

EDIT: It was my delta, I set it to 1 now it looks good! Still during launch I am getting gaps though. But might just need some fine tuning.

EDIT #2: Okay so it works great if I am just moving around - like perfectly! However I am more interested in during a launch. Which uses addforce. I get gaps when I launch from one corner to the opposite corner. Do you have any suggestions for that?

You need to calculate the spawn position.
E.G Take the last place you spawned and use MoveTowards the next point. Keep calling it till you have closed the gap.

So something like this?

var pos = playerRbody.transform.position;
       if(Vector3.Distance(lastUpdate, pos) > delta)
       {
           if(playerRbody.velocity.magnitude > 20)
           {
               GameObject _trail = Instantiate(trail_prefab, pos, new Quaternion(Random.Range(0, 360),Random.Range(0, 360),0,0));
               Vector3.MoveTowards(_trail.transform.position, lastUpdate, delta);
               lastUpdate = pos;
           }
       }

Ok try this

using UnityEngine;

public class Trail : MonoBehaviour
{
    public GameObject trailItem;
    public float delta = 10;

    Vector3 lastSpawnPoint;

    void Start()
    {
        lastSpawnPoint = transform.position;
    }

    void Update ()
    {
        while(Vector3.Distance(lastSpawnPoint, transform.position) > delta)
        {
            var spawnPos = Vector3.MoveTowards(lastSpawnPoint, transform.position, delta);
            Instantiate(trailItem, spawnPos, Quaternion.identity);
            lastSpawnPoint = spawnPos;
        }
    }
}
2 Likes

Excellent! With some very minor adjustments this is working perfectly for the application! I basically just added clearing of lastSpawnPoint to vector3.zero once the loop had completed, otherwise I was getting some strange results! Thank you very much!!

1 Like