- Home /
AddRelativeForce() causes unwanted turning
This is a repost of the stack overflow answer posted here: https://stackoverflow.com/questions/64945203/addrelativeforce-causes-unwanted-turning There's a bounty if you're interested.
I've create a movement script that moves a character (ship) in a topdown view. Gravity is disabled, but the rest of the settings i left mostly alone.
I'm applying a constant force in the forward or backward direction (local, so relative to the character NOT the world). This however causes some movement in other directions if the character is rotated for some reason.
Debug.Log(nameof(force) + force);
rigidBody2D.AddRelativeForce(force);
Lets me explain with pictures.
Starting point: Starting point
Moving forward somewhat (works okay): Moving forward
Back to starting point (stopping game, starting anew) And then i manually change the rotation to -90 in the inspector. I then move forward somewhat.
As we can see since the rotation is -90, moving forward in relative to the character (AddRelativeForce(..)) causes it to move to the right on the screen. The X
position increments accordingly.
However, also the Y
component changes, the character gets velocity in this direction, even though i apply no force in that direction - which we can see in the output.
How can I move my character with force without this issue?
Edit:
Here are the project files. Runs under the latest unity 2020.1.14f1
. The logic mentioned here is located in MoveExecuteSystem. I use a small ECS framework: https://github.com/Leopotam/ecs
https://wetransfer.com/downloads/b86dfb27b4388b85ac4fef285d87b39520201124193329/00af66
Your answer
Follow this Question
Related Questions
player jumps too high when clicking with two fingers. 2 Answers
Move an object via Rigidbody2D Physics and Transform at same time 0 Answers
2D AddForce towards direction with CONSTANT FORCE whether the direction is far or near. 1 Answer
About Rigidbody2D.AddForce's duration... 2 Answers
Gravity speed slows down when increasing screen size 1 Answer