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 solocs · Oct 31, 2018 at 06:14 AM · smoothdamp

No control over Vector3.SmoothDamp. How do I fix this?

I have this script on a prefab so once it's instantiated, it moves to the preset target. However, I'd like to control the speed of the SmoothDamp but no matter what values I change the smoothTime and smoothSpeed to, it's still the same velocity. However, when I test this code in a new project and use it, the parameters adjust the way that I want. I've double-checked if there was anything that would interfere with my SmoothDamp in my prefab but I can't seem to find anything. Here's the code:

 public Transform target;
 Vector3 currentVel;
 public float smoothTime;
 public float smoothSpeed;

 // Update is called once per frame
 void Start()
 {
     StartCoroutine("MoveToTarget");
 }

 IEnumerator MoveToTarget()
 {
     while (true)
     {
         if (Vector3.Distance(transform.position, target.position) > 0)
         {
             if (Vector3.Distance(transform.position, target.position) > 1.0f)
                 Debug.Log("FINISHED");
             transform.position = Vector3.SmoothDamp(transform.position, target.position, ref currentVel, smoothTime, smoothSpeed);
         }
         else
         {
             
         }
         yield return new WaitForSeconds(0.01f);
     }
 }
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
0

Answer by A_Lego · Oct 31, 2018 at 06:34 AM

Firstly, you made an infinite loop. I don't know if that was your intention, but I don't recommend doing that.

Secondly, the if statement is checking to see if the values are 1 unit away from eachother but it looks like your trying to move the transform TO the target, so it will only print "FINISHED" whenever it is more than 1 unit away from it's target. It's also going to continue to spam it every 0.01 seconds while it is greater than 1 unit away

Thirdly, it is highly recommended to not do anything taxing within Coroutines as they are not very good at handling high computation. I would recommend doing most of that in your Update method or possible even your FixedUpdate method.

Fourthly, your function is only running every 0.01 seconds, so your SmoothDamp is only being triggered that often.

Other than that, I don't see any issues with your code.

PS. the first note was just a suggestion, I could see uses for the infinite loop but I believe there are better ways to handle that as using the Vector3.Distance function can be a bit taxing on machines.

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

94 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

Related Questions

Using Mathf.SmoothDamp() in a foreach loop 1 Answer

how do i smooth out this movement c# 1 Answer

SmoothDamp smoothTime ignored! 1 Answer

How to apply smooth damp to movement? 1 Answer

Why smoothdamp can not work with TouchPhase.Ended? 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