Composite Collider 2D

Hello,

I am experiencing a problem with Composite Collider 2D. The player seems to fall off the platfrom and travel downwards to the distance, even though The collision is applied on the sum of the platform tilemap

image

Once I apply the OnJump message, the character seems to sky rocket

Hi StrawHat,

Are the CompositeCollider2D and the TilemapCollider2D non-trigger colliders? ‘Is Trigger’ must not be ticked. The player needs a non-trigger collider too.

Yes, they are not triggers. I am using unity 2022.3.6f1 LTS

Platform Physics Components

The very first problem is that the character falls off the platform the moment he “drops” to a lower platfrom level

Character Shorting Layer = Player
Tilemap renderer Shorting Layer = Platform

Character Physics Components

I think that there is some problem with the input system after all. Tried a different implementation (exposure of InputAction variable as shown in 3D unity course - Argon Assault) and the player moves a lot better.

However, the Composite Collider still allows the character to “fall into a different tileset”

In this case the collider allowed the character to fall underground at the edge

image

There are two issues that have nothing to do with your code:

For better physics calculations is better to set the characters’ Rigidbody Collision Detection setting to Continuos instead of Discrete, that would solve a lot of the problems you are having.

If the character still falls off the colliders, I suggest getting rig of the Composite Collider 2D and setting the colliders manually with Box Collider 2D. Composite Collider 2D basically creates a “hollow” collider, that can cause a lot of unexpected behaviours like the ones you are having, while the other colliders are treated as if they were filled, there’s no way to get inside a box collider, if the character traspasses it, the character will be pushed, preventing it from falling off.

Hope this helps!

Try to set the Collision Detection of the player’s Rigidbody2D component to ‘Continuous’, and ‘Interpolate’ to ‘Interpolate’.

Also try to disable the TilemapCollider2D and enable it again to make Unity recalculate the shape.

If you feel that the player gets pushes through the platform, disable all other game objects but the player and the platform. Maybe there is another (invisible) collider on top of everything that pushes the player around.

This is the problem when we add a collider for the cinemachine. Rick solves that problem by making the ‘cinemachine’ collider a trigger collider. Maybe you too have a big collider in your scene.

Have tried all suggestions, but the manual box colliders setup.

The problem seems to exist at the “L” shapes of the platform.

Nina, no the character is not pushed. The VERTICAL collision lines of the auto generated collider do not work. Only the horizontal lines do stop the character from falling down.

The only way so far to keep the composite collider and not have the character plow throught the platform is to set the Geometry type to polygon.

In this case the character still travels some space inside of the platform, but stops from “falling down”.

Update: even geometry type allows the character to travel “inside” the platform and walk the auto generated collider lines.

That’s very odd. I’d like to take a look at this. Could you please fill out our form and send me your zipped project (without the Temp and Library folders)? Don’t forget to add a link to this thread.

When you are done, drop me a line here, so I can check our server.

Hello,

I am very sorry. I had typed

void Run()
    {
        Vector2 playerVelocity = new Vector2(moveInput.x, body.velocity.y);
        rigidbody.velocity = playerVelocity * runSpeed; // <----

        bool heroIsMovingHorizontally = Mathf.Abs(body.velocity.x) > Mathf.Epsilon;
        animator.SetBool(runAnimation, heroIsMovingHorizontally);
    }

When Rick showed

void Run()
    {
        Vector2 playerVelocity = new Vector2(moveInput.x * runSpeed, body.velocity.y);
        rigidbody.velocity = playerVelocity; // <----

        bool heroIsMovingHorizontally = Mathf.Abs(body.velocity.x) > Mathf.Epsilon;
        animator.SetBool(runAnimation, heroIsMovingHorizontally);
    }

Also, I tried to implement the movement via the transform.Translate(), but this method also seemed to plow the character accros the collider lines,

Also tried to use rigidbody.MovePosition(), which left the character frozen

However, shouldn’t the colliders (not being a trigger) stop the character from going through the collision lines (e.g. via the transform.Translate() method)?

Nina I have corrected the code, but zipped the project for you to take a look, since it includes an additional implementation with no luck.

Essentially, only the way that Rick demonstrated does stop the character from passing the Vertical collision lines.

Thank you

Depending on the force that gets applied to a Rigidbody2D, and depending on the speed, transform.Translate() could override the changes of the physics simulation. For this reason, it is recommended to move a game object via the rigidbody instead of manipulating the transform directly. The data from the Rigidbody2D gets processed in the physics simulation, and the physics simulation assigns the new values to the transform.


// (1)
Vector2 playerVelocity = new Vector2(moveInput.x, body.velocity.y);
rigidbody.velocity = playerVelocity * runSpeed;

// (2)
Vector2 playerVelocity = new Vector2(moveInput.x * runSpeed, body.velocity.y);
rigidbody.velocity = playerVelocity;

(1) and (2) are not the same. In (1), the runSpeed value gets multiplied by moveInput.x and body.velocity.y. In the physics simulation, the player is always falling down. He rarely has exactly 0f velocity on his y-axis. By multiplying the y-velocity by runSpeed, the player gets an additional force, if you will, and that could explain why he is falling down with your code. The edge collider is just a thin line. It is easy to push the player beyond it. Since this is very likely the explanation for the behaviour in your game, and since the problem does not occur with Rick’s code, it is not necessary for me to take a look at your game.

For further information on the maths behind the quoted code, see ‘Multiplying a Vector by a Scalar’ on this website.

Ok, thank you very much, added the project just in case you wanted to take a look.

I also thank you for the link, this is definately useful!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms