How to add force in up direction of a 2d object in Unity

mukul

How to add force in up direction of a 2d object after it has rotated? I am using AddForce(transform.up) but it always moves the object upwards, irrespective of its rotation. I want to move a ship always in its forward direction even after it spins.

Jay

[Edit to include actual solution]

When using a Rigidbody you are agreeing to no longer modify the transform directly, and let it be controlled only by the rigidbody. By rotating using the transform you are not properly allowing the body to have angulart momentum, and can run into a host of differnet issues.

Instead, rotate your object through Rigidbody.MoveRotation(), or better: Rigidbody.AddTorque()


it's likely you're not actually rotating the transform and are instead rotating the sprite or a child of the transform. You can verify this by using:

You can also try adding the force relative, this should give you the correct direction:

rb.AddRelativeForce(Vector3.Up);
Which is guaranteed to apply the force in the direction the rigidbody is facing. If you have the same behaviour with this then the object with your rigidbody is not being rotated and therefore world and local up will be the same.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to add force in up direction at an angle in Unity

How to add force in (backward direction) to an moving object in Unity

How to make a game object move to the opposite direction of a constantly moving object? Unity 2D

How to add force to an object after a collision in unity2d, to simulate hitting a baseball with a bat

Unity 2D object face direction its moving

Object exits portal moving the wrong direction in Unity 2D?

How to add spinning force to unity2d game

push object with add force in Unity3D

How to rotate a Unity camera around an object in up-down direction with a degree input

Object only moving small amount in dragged direction [Unity 2D isometric]

How to move a game object in the forwrard direction of an other game object Unity3D

How to find the length of a 2D game object in Unity

How to avoid object references for spawned objects in Unity 2D?

How to rotate a 2D object by 90 degrees in unity

How to make object pull another in Unity 2D

Unity Rigidbody2d.addforce wont change direction after force is applied

How can I make 2d object constantly moving in one direction, but when given horizontal input change direction but not change speed?

How to change the horizontal movement direction of the player in Unity2D?

Unity3D - Move an object to the opposite direction by clicking on it

Unity - How to add GameObjects to an array and attach RigidBody2D so they move up and down

Unity code not allowing character (2D, rigidbody) to turn direction in air. How can I get more air control with this script?

Unity2D - How to rotate a 2D object on touch/click/press

How do I calculate the angular position of an object in relationship to another object in Unity 2D

How to addforce in a random direction to a rigidbody2d game object

Unity physics how to get the force vector that is working on an object

How to add a force 90 degrees / sideways from a forward vector3 direction

2D Object Collison Unity

Unity 2018: 2D Object - SpriteMesh

Add force to object by rotation