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
0
Question by StevenUnu · Sep 17, 2017 at 02:00 PM · targetsmoothdamp

Does SmoothDamp reach target?

This is rather a yes or no question but I would appreciate details. I'm wondering if Mathf.SmoothDamp and Vector3.SmoothDamp DO really reach their targets or just aproach to the target but never really reach it like Lerp. Also, if this is the case, what tricks will one use to get it to reach the target?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by hexagonius · Sep 17, 2017 at 02:55 PM

SmoothDamp is eventually reaching the target, but since the amount of adjustment is always smaller than what it takes to each it, or will only happen due to floating point precision.
You use it rather like a towrope, where it's not important to each any target.

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 pcbeard · Dec 30, 2019 at 12:47 AM

Here's how I'm currently using Vector2.SmoothDamp(), which may not be perfectly correct, but it does seem to do the job. I've written a generic smoother:

 struct Smoothed<T> {
     public T value;
     public T velocity;
     public delegate T smooth_t(T value, T targetValue, ref T velocity);
     static public smooth_t smoother;
 
     public void Smooth(T targetValue) {
         value = smoother(value, targetValue, ref velocity);
     }
 }

Since it has a delegate, you have to fill it in for the type you're trying to smooth:

 Smoothed<Vector2>.smoother = (Vector2 value, Vector2 targetValue, ref Vector2 velocity) => {
     value = Vector2.SmoothDamp(value, targetValue, ref velocity, 0.2f, Mathf.Infinity, Time.fixedDeltaTime);
     return velocity.sqrMagnitude <= 0.00001f ? targetValue : value;
 };

I'm passing Time.fixedDeltaTime, since I'm smoothing values observed from FixedUpdate() like this:

 public Vector2 position;
 private Smoothed<Vector2> smoothPosition;
 
 private void FixedUpdate() {
     if (smoothPosition.value != position) {
         smoothPosition.Smooth(position);
         if (smoothPosition.value == position) {
             Debug.Log("smoothPosition == position");
         }
     }
 }

As you can see, when the smoother delegate sees the velocity go nearly to zero, it finishes by assigning the target value to the value, so my code can reliably do an exact comparison with the target value. A better, but maybe more expensive test would be to finish when the Euclidean distance between the current and target values is nearly 0. In practice, this seems to work fine.

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 hexagonius · Dec 30, 2019 at 08:33 PM 0
Share

Small correction to your statement. You do not have to use fixedDeltatime in FixedUpdate, deltaTime IS that value during FixedUpdate.
It's only when you need to access fixedDeltaTime anywhere else that you actually need to call it directly.

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

70 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

Related Questions

SmoothDamp or Lerp with Rootmotion to match a target position 0 Answers

Quaternions, Eulers, and SmoothDampAngle 0 Answers

SmoothDamp won't reach its target 1 Answer

Smooth damp the lowpass frequency in coroutine: value only changing in 1 frame 1 Answer

iOS game: Is it possible to target iphone 4 and ipad only? 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