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 /
avatar image
1
Question by jusw85 · Feb 17, 2017 at 09:14 AM · inputmouselagjitter

Mouse input lag causes line rendering jitter

I have

  1. a 2D sprite that moves with a rigidbody2d,

  2. mouse input that provides a location in world space,

  3. a line renderer that draws a line between the sprite and mouse point.

When my sprite moves, the line jitters badly. I've attached a gif.

https://giphy.com/gifs/l3q2xdNZjKrlRlYu4

This is the simplest project that reproduces this error. Code sample below:

 void Update() {
     var moveInput = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));

     var rawMousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
     lookPosition = new Vector3(rawMousePosition.x, rawMousePosition.y, 0);

     lineRenderer.SetPosition(0, transform.position);
     lineRenderer.SetPosition(1, lookPosition);
 }

 void FixedUpdate() {
     rigidBody2d.velocity = moveInput.normalized * moveSpeed;
 }

Moving the sprite directly using it's transform, the line doesn't jitter, but the line still noticeably lags behind the mouse point.

https://giphy.com/gifs/l3q2Qwi29hWeSkG5y

 void Update() {
     var moveInput = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
     transform.Translate(moveInput * moveSpeed * Time.deltaTime);

     var rawMousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
     lookPosition = new Vector3(rawMousePosition.x, rawMousePosition.y, 0);

     lineRenderer.SetPosition(0, transform.position);
     lineRenderer.SetPosition(1, lookPosition);
 }

I can accept the line lagging behind the mouse point in the 2nd scenario; my question is how do I prevent the jitter in the 1st scenario, given that I'm moving rigidbodies in my 2D project?

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

1 Reply

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

Answer by FortisVenaliter · Feb 17, 2017 at 09:01 PM

It most likely has to do with the update order. You're probably updating the line position before the rigidbody updates the position. That would give you the jitter. Try moving your line update code to LateUpdate(); Read here for more info.

Comment
Add comment · Show 3 · 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 jusw85 · Feb 17, 2017 at 11:55 PM 0
Share

Got it working, thanks!

avatar image nitzanwilnai · Sep 23, 2018 at 07:31 PM 0
Share

I had the same problem and this fixed it for me. Thanks!

avatar image hellothxsorry_unity · Jun 08, 2020 at 07:25 PM 0
Share

Had the same issue. Spent 2 days trying to fix this and your solution is a life savior even after 3 years! Thank you!

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

7 People are following this question.

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

Related Questions

Mouse Input Lag 3 Answers

Smoothen Mouse Axis Input 2 Answers

Rigidbody interpolation problem 0 Answers

Input System Can't Catch Event on Update 0 Answers

How can I detect the mouse is moving through code? 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