Characters move in different direction using move_and_slide but glue together when collided

Godot Version

4.2.2.stable.mono

Question

I’m making a RPG 2d game and I use characterbody2d for enemies and player. Both enemy and player move using the method “move_and_slide”. CollisionShape2D: CapsuleShape.
The problem is:
For example, Here are enemy A (characterbody2d) and enemy B (characterbody2d). A is moving left while B is moving front. they collide and then both moving left front.(they are moving in different direction before collision) The enemy backward should keep moving in its direction but instead it’s moving to a mixed direction.
I am confused why and have no idea how to avoid the enemy backward being affected by another. I want the enemy to be pushed by another but not glued together.

Code

//Vector2 towards; towards is a vector in direction of destination of movement.
private void GetTowards(){
            const float biasLimit = 0.25f;
            float xBias = Position.X / areaBoundary.X;
            if(xBias < biasLimit && xBias > -biasLimit){xBias = 0;}
            float yBias = Position.Y / areaBoundary.Y;
            if(yBias < biasLimit && yBias > -biasLimit){yBias = 0;}
            towards.X = (float)random.GetRandomDouble(0,1.6) - (float)0.8 - xBias;
            towards.Y = (float)random.GetRandomDouble(0,1.6) - (float)0.8 - yBias;
            towards = towards.Normalized();
        }
        public override void _Process(double delta){
                   //towards is constant during the movement and is assigned elsewhere.
                    Velocity = GetVelocity();
                    MoveAndSlide();
        }


CharacterBody2D defaults to “Grounded” mode where it sticks to moving floor platforms (including other characters). You could disable the “floor layer” your characters live on, or if you are using top-down movement best to set them to “Floating” mode.

2 Likes

Thanks! It’s a top-down game. After setting Motion Mode to “floating”, my problem solved.

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