Home > Software engineering >  Proper GameObject structure and how to move the parent object, when the child is moving / contains t
Proper GameObject structure and how to move the parent object, when the child is moving / contains t

Time:04-24

I have several questions around this topic, but for certain reasons I ended up having to split the Player character into a parent object and several child objects. I'll try to focus on this question for now.

First of all, this relates to characters in a 2D Isometric game.

This lead to the parent object holding all the scripts, while the actual body (Ridigbody 2D, Animator, Sprite Renderer, Collider) are in a separate child object.

enter image description here

I control the player by altering the "Player Body" rigidbody velocity, as I've seen in plenty of tutorials - this works perfectly fine:

this.rigidBody.velocity = velocity;

Since the rigidbody is a child object, the parent is not moved with it.

The thing is, I need to be able to offset different animations and I found no other way to do so, than to separate SpriteRenderer and Animator from the parent object. So now, I have the choice between being able to offset animations, or move the parent object properly.

Clearly, I'm approaching this in a wrong way - but I can't find resources on how a proper GameObject structure for characters in a 2d isometric space should look. I've had the same issue with the "center point" of the character, which isn't the top-left corner of the game object.

My question is:

How should I best structure my character GameObjects to avoid this kind of issue, while retaining the ability to offset animations? Is everything stuffed in one massive game object, or is splitting things up the right approach?

Should I just keep the structure and move the parent with the child? This seems more like a hack than a solution.

I'm just having trouble figuring out the proper setup on how to do these things, and how professional games might approach this, since any tutorials I can find are always focused on single aspects and not on how to put them all together.

CodePudding user response:

Put the rigidbody in the root so that whenever it moves everything else will move along. Then you may separate the other components on the child objects.

Edit 1: Forgot to state that the main collider should be on the same gameobject that has the rigidbody (in this case, the root).

  • Related