• 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 Miyth · Mar 17, 2015 at 03:00 PM · spaceshooterspaceship

Strategies for Physics-based Movement for 2D Space RTS Controls?

Heya, I've been hammering out this problem for awhile, and I think I've reached a dead-end.

I'm trying to create a simple DC/RTS controller capable of steering a physics-controlled Spaceship to a target position & heading.

I've got the following two basic functions working:

MoveTowardsTarget()

RotateTowardsTargetDirection()

Both use Rigidbody2D.Addforce / AddTorque and calculate braking force per-frame in order to accelerate/slow the ship correctly.

Currently I'm just running both update functions simultaneously until the ship reaches the target position. Unfortunately, this means that if I give the ship a target location behind it and relatively close to it, the ship can never reach the target position, and instead orbits it.

I'm aware this code has problems, but I'm not sure what kind of "Crossover" algorithm I need to determine if the target location is too close to the ship to move-while-turning, based off of the turn-speed and movement speed of a specific ship.

 void UpdateMoveToPosition()
     {
         if(_moveTargetReached == false)
         {
             //Grab the Clicked position, set it's Z axis to match the same plane as the ship, then calculate relevant information.
             Vector3 CursorPosition = _targetPosition;
             CursorPosition.z = transform.position.z;
             Vector3 targetDir = CursorPosition - transform.position;
             bool TargetAhead = true;
             float AngleOffsetFromForward = Vector2.Angle (transform.up,targetDir);
             float DistanceRemaining = Vector3.Distance (transform.position,CursorPosition);
 
             if(AngleOffsetFromForward > 90 ){TargetAhead = false;}
 
             //If we're outside our margin of error for Heading and distance, then set our turnTarget to false.
             //This enables the update loop: RotateTowardsTargetDirection(). 
             if(AngleOffsetFromForward > 2f && DistanceRemaining > 10f && _turnTargetReached == true)
             {
                 _turnTargetReached = false;
             }
             //Once TurnTargetReached is false, it must be set true again in the "RotateTowardsTargetDirection" function. 
             if(_turnTargetReached == false)
                 RotateTowardsTargetDirection (targetDir);
 
             //Likewise, MoveTargetReached is false until we reach the target position.
             //This is where the logical flaw is. This Function only updates the ship along its own forward-facing axis. Right now, it updates regardless of the status of TurnTargetReached.
             if(_moveTargetReached == false){
                     MoveTowardsTarget(_targetPosition);
             }
 
             //Once the move target is reached, set the final heading of the ship, then abort this update loop.
             if(_moveTargetReached == true)
                 _finalHeading = transform.up;
         }
     }

The difficulty of the problem is compounded since both MoveTowardsTarget and RotateTowardsTargetDirection actually use forces to update the movement of the ship, and calculate braking force in order to slow/stop it. (Making them difficult to plug into, and temperamental due to the mismatches between simulated braking distance & the physics engine.)

Any advice or links to something I could read-up on to better understand the kind of algorithm / code that would be needed to make a true algorithm would be a life-saver. I'd hate to give up on true physics-based movement for the game, but I'm running out of ideas on how to make this function properly.

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 Helios_Forged_Games · Apr 02, 2017 at 10:09 AM

I would look into something like a PID controller. That way it will be able to adjust the values based on the predicted error. If you want to read up on it: https://en.wikipedia.org/wiki/PID_controller

I am currently in the process of programming one myself so I can't make specific recommendations as of yet about how to go about it. You may want to go search up a guide on how to go about programming it if you have difficulty understanding the calculations.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Space Shooter Player 2 ship is flipping when going into Play Mode 0 Answers

Freelancer of Freespace type Enemy Ai 2 Answers

Making Top-Down spaceship movement, getting current speed, acceleration, without rigidbody! 1 Answer

"AI"/Behaviour for a Waypoint system 1 Answer

3D spaceshooter : targeting system 3 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges