Search code examples
c#unity-game-engine2d

How to connect two game objects in Unity 3.3.0


I haven't really tried much because I don't know what to try. I am extremely new to Unity and coding in general, so I don't know how to do it with code, but I imagine that you wouldn't need code for it. By "Connect two game objects" I mean making them move in unison. E.g. Let's say I have a drop shadow for my player, but it's a separate object and when they collide with something, the character stops moving as it is pushed against a wall, but as the shadow has a different hitbox, it doesn't stop moving. It just keeps going without the player, which obviously looks kind of dumb. If I try to give them two separate hitboxes of the same size and shape, in the same location, it will glitch out as they are constantly colliding. I am using Unity 3.3.0 with C# in the 2d preset. Also, I'm using Visual Studio. I think it's 2019 community or something.


Solution

  • You can do this by making one game object a child so they will move in unison and then checking for a collision with the box collider

    void onCollisionEnter(Collision collision)
    {
        if (collision.gameObject.layer == "wall layer"
        {
            gameobject.transform.parent = null;
        }
    }