- Home /
How to make a Line Renderer follow the target without delay ?
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 : https://vimeo.com/560602897 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[i] = Vector3.Lerp(pointPoses[i], pointPoses[i - 1] + target.up * targetDistance, smoothSpeed * Time.fixedDeltaTime);
}
line.SetPositions(pointPoses);
}
}
Thanks in advance <3
Check what happens if you replace FixedUpdate
with Update
.
@DenisIsDenis thanks for the replay its working fine but its jittering the problem is my player is physics based so both of my player and cinemachine camera runs on fixed update thats why I changed it to fixed update on the first place
Answer by DenisIsDenis · Jun 10, 2021 at 08:35 AM
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[i] = Vector3.Lerp(pointPoses[i], pointPoses[i - 1] + target.up * targetDistance, smoothSpeed * Time.deltaTime);
}
line.SetPositions(pointPoses);
}
}
yeah after a lot of testing I ended up changing my whole player scripts and try it on a new scene with a normal camera and it worked perfectly (using update). I think the problem is with Cinemachine camera damping... because when I use a virtual camera without the follow target function it works fine, but as soon as I turn it on the jitter is back.. I think when the camera follow a target and then another object follow that same target it somehow intersecting with each other idk... at least I know where's the problem now XD. again thank u for everything so far Iam really grateful for everything <3
Answer by tuinal · Jun 09, 2021 at 07:34 PM
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).
Thanks a lot for the great explanation Iam just a bit confused on how to approach this.. the main problem is as you can see here https://vimeo.com/561040946 when I change it to update this happens. my player movement run on fixed update to handle velocities and other physics function that need per frame calculation and due to that I changed my Cinemachine brain update method to FixedUpdate and thats causing a jitter on the tail so I thought doing it in fixed update would solve it.
"sorry my english isnt that great I hope I made any since"
Answer by Harry_Drew · Jun 09, 2021 at 06:47 PM
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 :)
yeah I've set the first line renderer point to the target position pointPoses[0] = target.position;
but still somehow moves around. I also tried setting more than one point to the target Pos but its still not centered. I thought about using trail renderer but I want to make a sprite for the tail later the problem with trail renderer that its vanish over time and that would not achieve the look Iam after. anyway thanks for the replay I really appreciate it <3