• 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 drina7e · Feb 22, 2012 at 09:59 PM · movementphysicspathfindingclicktomove

Physics based click to move

Background: physics based top down, 2.5D space shooter (something like multiplayer Unitroids)

We have a physics based manual contol working well (fwd/back, stop, strafe, and turn)

We have left click to move to a place/orientation using the physics we developed in manual control, but we are having trouble scripting ship control to do it intelligently (overshoot, slowing down too soon, very inefficient paths, etc). It doesn't have to be a Unity script, just seeing a good algorithm would be very helpful.

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 Barsonax · Sep 04, 2016 at 04:00 PM

I made a method for this. Just be sure that all variables use the same time step (rigidbody.velocity for instance is in seconds while you apply your forces every fixed update which is 50 times per second).

I know this is a old question but iam sure you are not the only one wanting to do this.

         /// <summary>
         /// Tries to minimize the error. Useful when working with physics and trying to arrive at a position and similar problems.
         /// A example of such a problem would be a car that has to move to a goal and has to arrive exactly at that goal without overshooting.
         /// </summary>
         /// <param name="error"></param>The error. This is a value that indicates how much we are off our goal. Think of this as the distance to your goal
         /// <param name="deltaStep"></param>How much the error will change in the next step. Think of this as the current speed of your car.
         /// <param name="maxDeltaDelta"></param>The max amount of change that we can apply to deltaStep. Think of this as the acceleration of your car.
         /// <returns></returns>
         public static float CalculateArrival(float error, float deltaStep, float maxDeltaDelta)
         {
             double goalDeltaStep;
             var minInfluenceRange = deltaStep - maxDeltaDelta;
             var maxInfluenceRange = deltaStep + maxDeltaDelta;
             if(error > minInfluenceRange && error < maxInfluenceRange)
             {
                 goalDeltaStep = -error; //This prevents overstepping
             }
             else
             {
                 if (error >= 0)
                 {
                     goalDeltaStep = -Math.Sqrt(Math.Abs(2f * maxDeltaDelta * error));
 
                 }
                 else
                 {
                     goalDeltaStep = Math.Sqrt(Math.Abs(2f * maxDeltaDelta * error));
                 }
             }
             var difference = goalDeltaStep - deltaStep;
             return Mathf.Clamp((float)difference, -maxDeltaDelta, maxDeltaDelta);
         }
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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Click to move a character in a terrain 1 Answer

adding force 1 Answer

How to make a 2D Player move a box properly ? 1 Answer

Realistic player movement? 2 Answers

Best way to have GameObjects reach various targets at the same time 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