Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by N4ma3 · Jan 20, 2019 at 11:27 PM · linerendererfixedupdatejittery

Jittery Line Renderer in Fixed Update

I'm using a Line Renderer to draw a prediction of the trajectory of an object. This trajectory is physic based, but with custom behaviours (some planets or whatsoever are attracting the object).

The drawing of the trajectory works pretty fine, but for some odd reason the line jitters, as you can see on this gif: https://ibb.co/SV7LX5K

Here is my code for this line:

 private void FixedUpdate() => DrawLine();
 
 private void DrawLine()
 {
     for (var i = 0; i < _positionCount; i++) _lineRenderer.SetPosition(i, GetPosition(i));
 }
 
 private Vector2 GetPosition(int index)
 {
     var position = GetPreviousPosition(index);
     foreach (var attractor in _attractors)
     {
         _velocity += attractor.GetForce(position, _mass, out var insideCollider) * Time.deltaTime;
         if (insideCollider) return position;
     }
 
     return position + _velocity * Time.deltaTime;
 }
 
 private Vector2 GetPreviousPosition(int index) => index == 0 ? _startPosition : (Vector2) _lineRenderer.GetPosition(index - 1);

Let me explain this a little:

For each position of the line, I retrieve the position of the previous point of the line. If I'm dealing with the first point, I take the _startPosition which is the position of my player.

With this position, I calculate the force that every Attractors (the planets and stuff) will apply when an object of a certain mass is at a certain position (I won't show this code, it's a basic implementation of the universal law of gravity, and it works just as expected).

Then for each attractors, I add the calculated force multiplied by deltaTime to my _velocity. The initially value of _velocity is set by the slingshot thing, it's the actual force my player will have when you release the mouse button.

Then I return the previous position + the _velocity time Time.deltaTime. Also, if my current position is inside the collider of an attractor, I set all the next points of my line to this current position (to avoid going through objects with the trajectory line).

All this code seems to work really well, if I release the mouse, the player will indeed follow the drawn trajectory. But this line is jittery, and I can't find why... I tried to put all this code in Update, and even in LateUpdate, but it did nothing good. So if anyone have any idea, I'll gladly accept your help.

Thanks for your attention.

EDIT: I just realize that maybe it could be useful to know the settings of my line renderer, so here's a screenshot: https://ibb.co/v3Pw16X

Comment
Add comment · Show 4
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 xxmariofer · Jan 20, 2019 at 11:37 PM 0
Share

Your code looks fine, but should you be using fixedDeltaTime ? not sure if thats the problem but for sure using deltatime in fixedupdate will end up creating extrange behaviours

avatar image N4ma3 xxmariofer · Jan 20, 2019 at 11:43 PM 1
Share

Thanks, it does not change anything since in FixedUpdate Time.deltaTime is the same as Time.fixedDeltaTime. Time.deltaTime adapt to the context.

avatar image xxmariofer N4ma3 · Jan 21, 2019 at 12:01 AM 0
Share

Well after some test, you are right but i have to say that makes no sense, thanks for $$anonymous$$ching me something new :) whats the reason you are updating deltatime twice?

Show more comments

1 Reply

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

Answer by N4ma3 · Jan 21, 2019 at 12:07 AM

I just found what was my problem : This was not from the code of the drawing of the line, but from the code in which I drag the little square to launch it. To calculate the force to apply to this square, I multiplied all the operation by Time.deltaTime. But this operation was not done in a FixedUpdate (it's done in a OnMouseDrag function).

So my first velocity depended on the frame rate, rather than on the fixed frame rate.

Comment
Add comment · 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

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

165 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 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

What is the difference between Update() , LateUpdate() , FixedUpdate(), and when i should use them 3 Answers

Only Disable One Update on a Script 1 Answer

What is the most accurate way to call a function based on time? 0 Answers

Line Renderer with the pattern of a sprite 1 Answer

Draw a proper dotted line 0 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