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 DustedHam · Jul 08, 2013 at 03:56 PM · movementpositionvelocityaccelerationdeclaration

[C#] Accelerate then decelerate to its wanted position

I have an object that needs to accelerate towards a location then decelerate to a given position using velocity's.

Here's what I have.

 //Movement
     public float decelDist;
     public float distanceTo;
     protected virtual void Movement() //Called from FixedUpdate
     {
         
         float adjustedAccel;
         adjustedAccel = acceleration / rigidbody.mass;
 
         decelDist = ((new Vector3(Velocity.x * Velocity.x, 0, Velocity.z * Velocity.z)) / (2 * adjustedAccel)).magnitude;
         distanceTo = Vector3.Distance(transform.position, wantedPos);
         
         if(decelDist >= Vector3.Distance(transform.position, wantedPos))
          {
             //slow down
             Velocity -= (wantedPos - transform.position).normalized * adjustedAccel;
             Debug.Log("slow down");
         }
         else if (decelDist < Vector3.Distance(transform.position, wantedPos))
         {    
             Velocity += (wantedPos - transform.position).normalized * adjustedAccel;
             Debug.Log("speed up");
         }                
     }

In my mind this seems like it should work, however, it clearly doesn't. it has an tendency to overshoot, then head back, overshoot a lot (1/4 of stating distance ish) again, and repeat.

It does have to use that Velocity variable because other things will be effecting it later on.

Thank you, for all the help!

Comment
Add comment · Show 2
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 DaveA · Jul 08, 2013 at 04:01 PM 0
Share

You may be interested in a script that implements many interpolators which do what you want: http://wiki.unity3d.com/index.php?title=$$anonymous$$athfx and http://wiki.unity3d.com/index.php/Interpolate

avatar image DustedHam · Jul 08, 2013 at 04:10 PM 0
Share

Although they may get the effect I want, the velocity will be effected by other forces, so a simple slerp won't actually function here.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by sparkzbarca · Jul 08, 2013 at 04:45 PM

vectors cant be perfect. the slower you go the closer you'll get but at a certain point you need to either accept being "close enough" or if you want exact positioning you need to get very very close and teleport there.

use mathf.approximate to make sure your either "close enough" or close enough to teleport.

so

 if(mathf.approximate(current_postion,desired_position))
 {
 teleport or stop and accept it as good enough
 }
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

17 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

Related Questions

How do I create a rubber band effect on a camera forward movement? 1 Answer

Move Player without acceleration 1 Answer

Spaceship movement with acceleration and deceleration on Unity 0 Answers

Acceleration problems (don't know how to make it stop moving) 0 Answers

When does a pos of a gameObject change from its velocity? 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