Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Zero101 · Jan 10, 2018 at 05:39 PM · c#cameraplayer movementlerpingienumerable

Player movement with lerp is jerky.

Hey!I have a question! I've got this piece of code:

         ...
         if (TargetChanged(Target, TarLastPos))
         {
             if(isLerping)
             {
                 StopCoroutine("LerpPlayer");
             }
             StartCoroutine(LerpPlayer(0.5f));
         }
     }
     //FUNCTIONS
     bool TargetChanged(Transform target, Vector3 lastPos)
     {
         if (
             //Target moved a tad
             Mathf.Abs(target.position.x - lastPos.x) > 0.01f ||
             Mathf.Abs(target.position.y - lastPos.y) > 0.01f ||
             Mathf.Abs(target.position.z - lastPos.z) > 0.01f
             )
         {
             return true;
         }
         else
         {
             return false;
         }
     }
     //Move player so it is in fron of camera
 
     IEnumerator LerpPlayer(float duration)
     {
         isLerping = true;
         Vector3 StartingPos = Player.transform.position;
         float StartTime = Time.time;
         float MovementPercent = 0f;
         while (MovementPercent < 1)
         {
             MovementPercent = (Time.time - StartTime) / duration;
             this.transform.position = Vector3.Lerp(StartingPos, Target.position, MovementPercent);
             yield return null;
         }
         isLerping = false;
         this.transform.position = Target.position;
     }

What it should do:It is meant to make the player's position get lerped to where the camera is pointing(indicated by the Vector3 Target) and because it is a Lerp, I need it to be in a IEnumerator,but that also means to not cause imense lag i set up a function to detect if the camera(Target) changed and so it stops the coroutine and start's it again, this time having the new Vectors.

What it does: It works...sort of. The player is "jolty" indicated in the gif, and I am not quite sure why, it should be smooth..

P.S. I couldn't find any question/answer that solved my problem yet, but I might have missed the answer i was looking for.

Gif: https://gyazo.com/0ade1715343fc82ce4a580359a7f4620

[preview of external content removed for GDPR compliance as it was including 3rd party cookies]

Comment
Add comment · Show 3
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 TreyH · Jan 10, 2018 at 05:46 PM 0
Share

Ins$$anonymous$$d of yield return null have you tried yield return new WaitForEndOfFrame() or yield return new WaitForFixedUpdate(), with the latter for when your object is physics-based?

avatar image Zero101 TreyH · Jan 10, 2018 at 06:47 PM 0
Share

Doesn't seem to help. With the EndOfFrame it doesn't lerp, and with FixedUpfate, well, you can look at the gif: https://gyazo.com/28a2541aa018ec4a21bbf24a30c451db

avatar image TreyH · Jan 10, 2018 at 07:03 PM 0
Share

If you print out $$anonymous$$ovementPercent during the coroutine, does it ever break 1? You have it set up to break out once it's beyond 1, but the movement will still happen with your current ordering.

1 Reply

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

Answer by AmazingRuss · Jan 10, 2018 at 06:53 PM

Looks like you are running the camera and object in different update loops.

Try yielding on WaitForFixedUpdate instead.

If this is for mobile, yielding like that is a performance problem, and you'd be better off putting it in the FixedUpdate function.

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 Zero101 · Jan 11, 2018 at 05:56 PM 0
Share

It kinda works, I changed it from Late to Fixed and ditched the IEnumerable, and ins$$anonymous$$d of Lerp i used $$anonymous$$ove To.

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

444 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Lock player movement within screen bounds 2 Answers

Moving player relative to non-child camera 1 Answer

Distribute terrain in zones 3 Answers

Not able to make fps character controller player movement.,Not able to make Fps character controller player movement properly. 0 Answers

Move player based on camera direction (MOBILE JOYSTICK) 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