So I was trying to figure this out for a while and I cant see where’s the problem in this Iam new to this and I will appreciate if someone can help me with it… Iam making a LineRenderer tail where my follow target is a child of my player and the rest of the line points following the previous points with a specific distance and speed. the problem is the first point position should be moving with the player seamlessly but the LineRenderer first point somehow keep delaying when the velocity of the player increases like this :
and here’s my code:
public class Tail : MonoBehaviour
{
public int length;
public float targetDistance;
public float smoothSpeed;
private Vector3[] pointPoses;
public LineRenderer line;
public Transform target;
private void Awake()
{
line.positionCount = length;
pointPoses = new Vector3[length];
}
void FixedUpdate()
{
pointPoses[0] = target.position;
for (int i = 1; i < pointPoses.Length; i++)
{
pointPoses <em>= Vector3.Lerp(pointPoses_, pointPoses[i - 1] + target.up * targetDistance, smoothSpeed * Time.fixedDeltaTime);_</em>
}
line.SetPositions(pointPoses); } } Thanks in advance <3
I think you need the first few line render points to be at the target position? Its worth a try at least. If that doesn’t work you could always use a trail render which would be better suited for this situation.
Hope this helps
I tested the script and found that if you replace FixedUpdate with Update and change Time.fixedDeltaTime to Time.deltaTime, the result will be the smoothest. In my case, even after that, the tail trembling remained and I found the reason.
It turns out that the way the player moves affects how your script works: I, for example, first used the CharacterController, but it moved jerky. After changing the move script, based in the second case on the Rigidbody, the trembling of the tail stopped.
Modified script:
using UnityEngine;
public class Tail : MonoBehaviour
{
public int length;
public float targetDistance;
public float smoothSpeed;
private Vector3[] pointPoses;
public LineRenderer line;
public Transform target;
private void Awake()
{
line.positionCount = length;
pointPoses = new Vector3[length];
}
private void Update()
{
pointPoses[0] = target.position;
for (int i = 1; i < pointPoses.Length; i++)
{
pointPoses <em>= Vector3.Lerp(pointPoses_, pointPoses[i - 1] + target.up * targetDistance, smoothSpeed * Time.deltaTime);_</em>
FixedUpdate runs less frequently than update (it’s rate is set in edit>project settings>time).
It’s purpose is to offload things that are expensive and don’t necessarily need per-frame calculation. But a line renderer tracking a point will visibly stutter. I think you’re trying to smooth this with a lerp, but the lerp itself is not happening per frame as it’s in FixedUpdate.
Hypothetically, if you had some complex calculation, you’d still need to interpolate in Update with FixedUpdate doing the complex calculation to provide a velocity of the point you could dead-reckon with (this is how the physics system works). But you’re not doing a complex calculation.
Hence, just changing this to Update and Time.fixedDeltaTime to Time.deltaTime would probably fix it, if the code itself is working (which is hard to see from just this snippet).