Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by N4AZ · Jun 08, 2021 at 10:48 PM · lerplinerenderer

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DenisIsDenis · Jun 09, 2021 at 02:32 AM 0
Share

Check what happens if you replace FixedUpdate with Update.

avatar image N4AZ DenisIsDenis · Jun 09, 2021 at 06:12 PM 0
Share

@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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);
     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image N4AZ · Jun 10, 2021 at 05:46 PM 1
Share

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

avatar image
0

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).

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image N4AZ · Jun 09, 2021 at 08:24 PM 0
Share

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"

avatar image
0

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 :)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image N4AZ · Jun 09, 2021 at 07:14 PM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

122 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to get a smooth curved line between two points like those present between the nodes of bolt visual scripting. 1 Answer

Linear interpolation crash 0 Answers

Lerp Never Reaches Target 2 Answers

LineRenderer (strange bug) 1 Answer

Get positions at equal intervals between two Vector3 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges