1
\$\begingroup\$

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.

\$\endgroup\$
5
  • \$\begingroup\$ Unity is deprecating unityscript, did you mean to tag your question with c#? \$\endgroup\$
    – Vaillancourt
    Oct 2, 2018 at 14:08
  • \$\begingroup\$ Have you considered just rearranging your scene to work in the xy plane instead, if your physics are meant to be 2D? \$\endgroup\$
    – DMGregory
    Oct 2, 2018 at 14:23
  • \$\begingroup\$ How do I do that? \$\endgroup\$ Oct 2, 2018 at 17:27
  • \$\begingroup\$ 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. \$\endgroup\$
    – DMGregory
    Oct 2, 2018 at 17:48
  • \$\begingroup\$ I've edited my question, please help me \$\endgroup\$ Oct 4, 2018 at 17:22

3 Answers 3

1
\$\begingroup\$

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

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
-1
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ I will try out your solution and reply \$\endgroup\$ Oct 2, 2018 at 17:28
  • \$\begingroup\$ This answer is incorrect. It is perfectly possible to have a game object with a 3d MeshRenderer, but a 2d rigidbody and 2d collider. \$\endgroup\$
    – Philipp
    Jan 31, 2020 at 14:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .