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 ElectricMonk · Nov 19, 2015 at 06:06 PM · movementphysicsphysics2d

Translate to a target goal position and facing direction and stop

Hi There,

I have a 2d, top down navel game I've been working on and am having trouble with pathing the ship to a target position.

I have the speed set, it adds increments of acceleration and deceleration and can move to a target point and stop right now (so long as its moving in a straight line). I'm sure its a solvable problem as I know both the velocity, the changes in velocity, and the rotation speed ( which is currently fixed and being slerped). The translate is applied as a forward linear translate relative to the ship (no lateral or reverse movement) I have two problems tripping me up now.

  1. When we start not facing the target, the arc of the movement means I overshoot to goal and end up rotating endlessly around the goal. This is the main problem. This occurs when the beginning direction does not point a the goal, I basically just need to calculate an arc from one point & direction to a target point and direction. I've been thinking its basically calculating a bezier curve maybe, I'm just not really sure how to approach it.

  2. Ideally I'd like to adjust the trajectory so that I reach the target goal facing the target direction as well, but I'd settle for just reaching the target, stopping and then rotating in place. Mostly its problem #1 that concerns me. Even if someone can point me in the right direction, I was thinking either using a bezier curve to create the path from end goal to current position.

Alternatively, if anyone can suggest a 2d pathfinding kit on the asset store that could do this, that would be great. I'm using Nav2d right now which is great but it moves laterally causing the ship to drift and turn in strange ways.

The current speed is set directly from the acceleration and deceleration values, which are constant, and the rotation is a constant slerp. Any help or insight into this would be greatly helpful, thanks!

              // get direction to target
                 Vector3 vectorToTarget = targetPosition - transform.position;
                 angle = Mathf.Atan2(vectorToTarget.y, vectorToTarget.x) * Mathf.Rad2Deg;
                 Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);
                 transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime * rotateSpeed);
     
                 // move to target
                 if( distanceToTarget > stoppingDistance ){ 
     
                     // calculate current speed with acceleration
                     currentSpeed = currentSpeed + (acceleration * Time.deltaTime);
                     currentSpeed = Mathf.Min ( currentSpeed, maxMovementSpeed);
                     
                 }else{
                     // decelerate
                     currentSpeed = currentSpeed - (deceleration * Time.deltaTime);
                     currentSpeed = Mathf.Max (currentSpeed, 0f);
                 }
 

alt text

500.jpg (15.7 kB)
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

0 Replies

· Add your reply
  • Sort: 

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

37 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

Related Questions

Calculate initial velocity for projectile to reach point at a given angle and calculate angle to reach point at a given velocity. 1 Answer

Child GameObject can't use MovePosition on parent's Rigidbody2D? 3 Answers

Is there a standardized way for creating a 2D ball physics movement method? 0 Answers

Need help with my movement script.. :( 1 Answer

Moving a non-player object in a random direction which bounces off other objects. 2 Answers


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