Hello,
I am making a game similar to Flappy Bird, where the player stays in place (only moves up and down), but the obstacles scroll(to the left). I wanted to make a trail for my player, but since the player isn’t actually moving this proved rather difficult. Currently, I am using a Line Renderer
that makes the trail.
My code:
using System.Collections.Generic;
using UnityEngine;
public class TrailLine : MonoBehaviour
{
public LineRenderer line;
public float minDistance;
public float scrollSpeed;
public List<Vector3> lineVertices = new List<Vector3>();
void Start () {
lineVertices.Add(transform.position);
lineVertices.Add(transform.position);
}
void Update()
{
lineVertices.RemoveAt(lineVertices.Count - 1);
for (int i = 0; i < (lineVertices.Count); i++) {
lineVertices[i] = new Vector3(lineVertices[i].x - scrollSpeed * Time.deltaTime, lineVertices[i].y);
}
addCurrentPosition();
line.SetPositions(lineVertices.ToArray());
}
public void addCurrentPosition() {
lineVertices.Add(transform.position);
line.positionCount = lineVertices.Count;
}
}
The addCurrentPosition
method is called whenever the player changes direction, and thus an extra point is added to the line. This works well, however there is some weird “bouncing” that I do not understand.