# Adding force to a 3d object in a 2d scene

I have a 3d game object placed in a 2d scene (top-down view) in Unity. I want to add force to the object to move it in the direction it's facing. I attached a RigidBody2D component to it and when I use the AddForce() method, it's not working as I want it to i.e. it's moving on only one plane x or y but not in the facing direction. Please help me. Edit :- The game is my version of Asteroids. The ship is 3d and the asteroids are 2d. The scene is in 2d. I fixed the 'y' and 'z' rotation of the ship. Now how do I add force to the ship in the direction its facing ? The Update method -

void Update() {

    float rotationInput = Input.GetAxis ("Rotate");
if (rotationInput != 0) {
float rotationAmount = RotateDegreesPerSecond * Time.deltaTime;
if (rotationInput < 0) {
rotationAmount *= -1;
}
ship.transform.Rotate (Vector3.down, rotationAmount);
}
Vector3 currentRotation = ship.transform.eulerAngles;
currentRotation.x *= Mathf.Deg2Rad;
thrustDirection = new Vector3 (Mathf.Sin(currentRotation.x) * -1, Mathf.Cos(currentRotation.x), Mathf.Cos(currentRotation.x));


}

The FixedUpdate method -

void FixedUpdate() {

    if (Input.GetAxis ("Thrust") > 0) {
print (thrustDirection);
ship.AddRelativeForce (ThrustForce * thrustDirection, ForceMode.Force);
}

}


Thanks in advance.

• Unity is deprecating unityscript, did you mean to tag your question with c#? – Alexandre Vaillancourt Oct 2 '18 at 14:08
• Have you considered just rearranging your scene to work in the xy plane instead, if your physics are meant to be 2D? – DMGregory Oct 2 '18 at 14:23
• How do I do that? – Richard David Oct 2 '18 at 17:27
• Rotate anything that's on the xz plane 90 degrees, so it's in the xy plane instead? If you want more specific advice, edit your question to show us the specific situation you're dealing with. – DMGregory Oct 2 '18 at 17:48
• I've edited my question, please help me – Richard David Oct 4 '18 at 17:22

## 3 Answers

You should probably have look at the space shooter tutorial : https://unity3d.com/fr/learn/tutorials/s/space-shooter-tutorial

If i'm remembering it correctly they are using Unity RigidBody (not the 2d one) and they are freezing the y axis. You should probably look at "Moving the player" section

It is a 3D gameObject. 2D Rigidbody component can be added on it but will not work on it. Use a 3D Rigidbody component and add force only in the X-Z plane.

• I will try out your solution and reply – Richard David Oct 2 '18 at 17:28

mostly its type of your camera that makes your game 2d or 3d. RigidBody2d as in the name, only can move the object in (X,y) space and z is locked.

you cant use RigidBody2d and RigidBody in same simulation as they can not interact with each other and work differently as Rigidbody2d is made with Box2d engine and rigidBody is wrapper of Physx engine.