The parent object has a Rigidbody, the child object catches it but acts like static

Hi, in my game I have 4 units: Archers, Cavalries, Spearmen and Swordmen. Each unit has Rigidbody2D and BoxCollider2D (it’s trigger). I create soldier when the battle starts. Each soldier has CapsuleCollider2D and it’s not a trigger.

Normally, when two units collide with each other, I want the soldiers to push each other and the others to pass through. But as seen in the picture, they do not push each other, they push the unit object. Each soldier behaves like a static object. What could be causing this and how can I make them push each other the way I want?

9516373--1341667--upload_2023-12-7_15-12-36.png

You need to explain in more detail what Rigidbody2D and Colliders are involved here.

All I see here is a box and capsule colliders.

Only Rigidbody2D move, Colliders are attached to them for the sole purpose of detecting collisions.

How can a solider move independently if it doesn’t have its own Rigidbody2D?

A and B have Rigidbody2D and BoxCollider2D. A has a velocity and when A hit to B, A will push to B, right?
Now B’s collider trigger now and it has a child object, the child has a CircleCollider2D. A hit to B’s child but A can’t push B. But child’s collider get the B’s Rigidbody, so I think A can be push to child.

9517909--1342132--upload_2023-12-8_11-15-0.png

The above situation is also the case in my project, there is a parent object and this object has a Rigidbody2D component. There are Soldiers as child objects inside this main unit. Those with the Soldier CapsuleCollider2D component.

As I mentioned above, since the Soldier took the Rigidbody component of the main unit, I thought the Soldiers would be able to push each other and pass between them, but it did not happen. What do I need to do to do something like this?
9517909--1342135--upload_2023-12-8_11-15-11.png

Do I have to give Rigidbody to all Soldiers? There are 2400 Soldiers in total in the war, will giving Rigidbody2D to all of them cause problems in terms of optimization?

Please, let’s keep this simple so before getting into this A/B & hierarchy stuff (it’s just distracting from the basics), please explain what behaviour you need first.

This is the part you’re not spending time explaining and it’s the most critical for me to know. What does this mean? Do you want each solider to move independently of each other solider? If so then it should be very clear that each needs to have its own Rigidbody2D because as you should know, this is what moves in physics. Colliders don’t move independently of the Rigidbody2D they are attached to; they cannot be positioning in the world explicitly. They are only there to detect collisions.

If you want them to stay in the arrangement you have and all move in unison then yes, they can share a Rigidbody2D.

Yes, that’s a lot of Rigidbody2D so this won’t scale well if this is what you need to do.

OK, let me try to elaborate further.

First of all, I have a main object and let’s call it the army. The Army has Rigidbody2D and BoxCollider.

There are 100 soldiers in the army. Each soldier has the CapsuleCollider2D component.

The army has its own movement system and moves with rigidbody.velocity towards the enemy it has determined when the war begins.

At this moment, soldiers are floating like ghosts and passing through each other. I wanted to avoid this situation, I want to make the soldiers as if they were living creatures. When two armies collide, the soldiers should also engage each other, or if it is their own army, let them try to push past each other.

I also want to give soldiers the right to act independently. The soldier must be able to move in a circle that remains true to his starting position in the army. For example, two armies are fighting each other from the front, and when another enemy army approaches the player’s army from the side, the soldiers on the side must be able to move towards the enemy army.

I hope I could explain it this time.

Everything you’re describing sounds way, way out of the domain of Unity’s physics system. Or at least Unity’s normal physics system.

Wanting to simulate that many units, you’d probably have to look at proper RTS AI stuff, or look at Unity’s DOTS solutions to handle numbers of this scale.

So when I asked “Do you want each solider to move independently of each other solider?” the answer is “yes” correct? It would seem then you didn’t understand the basics of what I’ve said twice (three times now) in that a Rigidbody2D is what moves in physics, colliders are only there to detect collisions. You cannot have a single Rigidbody2D and it somehow control the positions of multiple other things; that would make no sense.

I would suggest first understanding how movement in physics works first then before you try doing this at scale because as you said:

… this doesn’t given each solider independent movement.

Also, the reply above would be the best advice because, depending on the scalle you’re doing this, performance can be a problem but still, you need to understand the basics first.

Thank’s man, I’ll look at them.

Ok, I haven’t used anything like this before, so I didn’t fully understand the subject, now I understand it a little more.

By the way, just to be a little fancy, I gave Rigidbody to all the soldiers and there were 2400 Rigidbody in total, it was a bit funny. :smile: