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 Theacesofspades · Jan 15, 2014 at 11:55 PM · c#lerpgun

Why is .Lerp so bumpy?

I am trying to make my own gun script and for some reason my gun is acting super bumpy. My script is:

     public Transform gunHolder;
     public float holdSide = 0.3f;
     public float holdHeight = -0.2f;
     public float moveSpeed = 1;
     public float rotateSpeed = 1;
     
     
     void Start ()
     {
         
     }
     
     void Update ()
     {
         gunHolder.localPosition = new Vector3(holdSide, holdHeight, 0);
         
         transform.position = Vector3.Lerp(transform.position, gunHolder.position, moveSpeed);
         transform.rotation = Quaternion.Lerp(transform.rotation, gunHolder.rotation, moveSpeed);
     }

Does anyone know a better way of doing this? Thanks.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by gfoot · Jan 16, 2014 at 12:12 AM

It could be partly due to different frame durations. You can test that by adding a random-length delay to your Update function and observing how well your game deals with it.

To compensate for that, for the last parameter to either Lerp function, pass:

 1 - Mathf.Exp(-k * Time.deltaTime)

Here 'k' is a constant that controls the lerp rate.

Some people use a multiple of Time.deltaTime as the third parameter here, but that is wrong for Lerp - you should use an exponential of the form I stated above, to properly correct for varying frame times.

Beyond that, if the target object is moving with a fairly constant speed then you'll still get some jitter, and can do even better with an alternate lerp function. See this thread for more details:

http://forum.unity3d.com/threads/130920-How-to-smooth-damp-towards-a-moving-target-without-causing-jitter-in-the-movement

and the webplayer demo I uploaded here (the bottom cube uses my custom lerp function):

http://www.gfootweb.webspace.virginmedia.com/SmoothDamp/WebPlayer.html

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

Answer by HappyMoo · Jan 16, 2014 at 12:26 AM

moveSpeed = 1 means you jump in one frame without smoothing. Try 0.1f for starters and play around with it till it feels right

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 HappyMoo · Jan 18, 2014 at 01:45 PM 0
Share

Did that help?

avatar image
0

Answer by KellyThomas · Jan 16, 2014 at 12:30 AM

The third parameter should be a number between 0 and 1.

If you imaging a line going from the first parameter to the second parameter, the third parameter represents percentage of progress along than line that is used for the return value.

Here is a table that may help visualise the concept.

first second third return 10 20 0.00 10 10 20 0.25 12.5 10 20 0.50 15 10 20 0.75 17.5 10 20 1.00 20

For a lerp to look smooth and even it should a stable start and end values with a "`t`" value that ranges between 0 and 1.

To take your postion lerp as an example:

  • The start value (`transform.position`) is changing as it is updated every time.

  • The end value (`gunHolder.position`) may be changing (player input?).

  • The "`t`" value is fixed.

This would mean that you would be move a fixed percentage closer to the end value on each frame.

This is an example with a "`t`" of 0.5 (to make the maths easier I am treating start & end as stable) :

frame# first second third return delta 1 10 20 0.5 15 5 2 10 20 0.5 17.5 2.5 3 10 20 0.5 18.75 1.25 4 10 20 0.5 19.375 0.625

As you can see the rate of movement (delta) starts high but quickly tends towards zero. For this use of lerp the trick to to find a "`t`" value that is not too high and not too low, it involves manual tweaking and a judjment call.

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

21 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

Related Questions

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

Gun locator script isnt working 1 Answer

Pushing More GUI Clips Than Popping 0 Answers

Question on Forcing a Specific Rotation 1 Answer


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