0
\$\begingroup\$

I am trying to create a space capsule that has four rockets to balance the capsule.

Capsule with four balance rockets

The rockets and capsule haveRigidbody2D and BoxCollider2D components attached. The rockets have a script that calls rbody.AddForce() when the relevant key is pressed. Rockets are child objects of the capsule object.

But it is not working because rockets are separating when the capsule is rotating.

I added joints to connect the capsule and rockets, but this time the capsule stopped rotating.

I've tried to use all joint types, but I could not find a suitable joint. The objects are 2D. I guess that a script can be coded that changes the rotation of the capsule and its position based on calculations, but if it is possible, I want to not calculate this manually.

How can I create a game object that is used for adding force, and combine these objects with another object?

\$\endgroup\$
3
  • \$\begingroup\$ Have you considered removing the Rigidbody2D components on the rockets, and calling AddForceAtPosition() on the parent body instead? You should also show us how you configured the FixedJoint2D when you tried using it. \$\endgroup\$
    – DMGregory
    May 8 at 22:18
  • \$\begingroup\$ I added all over again and It works well with FixedJoint2D. I think that I added freeze rotation on rigidBody2D wrongly before. \$\endgroup\$
    – uuur
    May 9 at 4:06
  • \$\begingroup\$ Want to explain your solution as an Answer below, to help future users with similar problems? \$\endgroup\$
    – DMGregory
    May 9 at 5:07

2 Answers 2

1
\$\begingroup\$

I would not give the rocket nozzles their own rigidbodies. I would instead attach them as children to the main rocket rigidbody so the whole thing becomes one single physics object. Then I would have the rocket nozzles use rigidbody2d.AddForceAtPosition in order to add forces not acting directly on the center of mass of the rigidbody but acting on the position of the respective nozzle.

Here is an example script. Put this on the "nozzle" game objects. Do not add a Rigidbody2D to the nozzles. Parent them to the rocket with the Rigidbody2D. The force is applied at the center of each nozzle and the x axis (red arrow) is the direction in which the force gets applied. Then set the desired thrust intensity and the key to control each nozzle in the inspectors of the nozzles.

using UnityEngine;

public class RocketNozzle : MonoBehaviour {
    private Rigidbody2D rb;
    public float thrust; // thrust power
    public KeyCode key; // keyboard key to activate thrust

    void Awake() {
        rb = GetComponentInParent<Rigidbody2D>();
    }

    void FixedUpdate() {
        if (Input.GetKey(key)) {
            var thrustVector = -transform.right * thrust;
            rb.AddForceAtPosition(thrustVector, transform.position, ForceMode2D.Force);
        }
    } 
}
\$\endgroup\$
3
  • \$\begingroup\$ I tried both of them and they are not behaving the same. While using AddForceAtPosition, if I run upper left and lower right rockets, Capsule would not spin, but instead swing. While using AddForce with rigidBody, the capsule would spin. I think that positions are symmetric. \$\endgroup\$
    – uuur
    May 9 at 12:26
  • \$\begingroup\$ @uuur I've added an example script to the answer. It works the way I would expect it in my example project. \$\endgroup\$
    – Philipp
    May 9 at 15:47
  • \$\begingroup\$ Yes. It is working. I guess that I used the force vector wrongly. I use new Vector2(1, 0) as force but it should be transfrom.right or i should use transform.TransformDirection() method. On debug, trustVector always changes with rotation but new Vector(1,0) does not change obviously. \$\endgroup\$
    – uuur
    May 9 at 21:44
0
\$\begingroup\$

If you connect the rockets to the capsule with a FixedJoint2D and call

rbody.AddForce(new Vector2(rocketPower, 0))

in the rocket's script, it will work well.

Be sure to not tick any constraints, and set gravity scale zero.

\$\endgroup\$

You must log in to answer this question.

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