2d RigidBody suddenly stops moving

Error Description:

I am creating a simple 2d platformer.

I have a Player GameObject with a sprite renderer, rigidbody2d, 2 circle colliders, 5 box colliders, and 2 scripts attached. One script was a character controller I got from github, the other is a player script that calls functions from the controller script.

The CharacterController has a Move method that adds velocity to the player depending on the key pressed to initiate the motion (right or left).
However, I had an issue where the Move method would be called correctly but the player wont move. Pressing a single direction button (right for example) would propel the player normally, until suddenly the character stops and wont move in that direction anymore. The only way to undo that is to move in the other direction then back and again. Note that the player also stops mid-motion (removing drag lets the player slide on a straight path even without pressing any key as long as some force has been added before, the issue causes the character to instantly stop even if it is sliding).

So I created a much simpler Controller script from scratch, and used AddForce instead. But I am facing the same EXACT issue.

I have no idea what the issue is, the code is simple enough, and if the issue was with the colliders, I should be stopped at a certain point, but I am able to access all parts of the tile map I created if I change directions then go back again.

Code:

public class PlayerMovement : MonoBehaviour
{
    public CustomCharacterController custCtrl;

    float horizontalMove=0f;

    // Update is called once per frame
    void Update()
    {
        if(Input.GetKey("right")){
            horizontalMove = 1f;
        }else if(Input.GetKey("left")){
            horizontalMove = -1f;
        }
    }
   
    void FixedUpdate()
    {
        custCtrl.Move(horizontalMove, false, false);
    }
}
public void Move(float move, bool crouch, bool jump){
        if(characterGrounded){
         //this is true, I checked using Debug.Log
            Vector2 fToAdd = new Vector2(move * moveForce, 0f);
            characterRB.AddForce(fToAdd, ForceMode2D.Impulse);
          
            // If the input is moving the player right and the player is facing left...
            if (move > 0 && !m_FacingRight) { Flip(); }
            else if (move < 0 && m_FacingRight) { Flip(); }
        }
    }

How are you setting characterGrounded? Have you monitored the value of “characterGrounded” and seen if it maybe switches to false at the same moment your movement stops working?

I monitored it using Debug.Log()
I set it using groundCollider.IsTouching(tilemapCollider)
Where groundCollider is one of the box colliders(the one at the bottom of the sprite) and tilemapCollider is the, as the name suggest, the tilemap collider.

Bump

Fixed the issue.

Dont juts use tilemap colliders with tilemaps. Add tilemap collider then a composite collider. In tilemap collider (in inspector) check “Used by composite” option. This will create one collider around the tiles rather than a collider for each single tile.

My issue was that my character collider kept getting stuck at the edges between each tile.

20 Likes

oh my god thank you so much

2 Likes

I have tried this and nothing helped anyway someone can help me with this issue

Please post your own thread. Don’t read a title of an existing thread, assume it has anything to do with yours and post an image asking for help without clarifying what is wrong etc.

As MelvMay pointed out, please start your own thread.

When you do, remember nobody here can read your mind.

This means YOU must communicate very clearly what is happening.

Here is how to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

How to understand compiler and other errors and even fix them yourself:

https://forum.unity.com/threads/assets-mouselook-cs-29-62-error-cs1003-syntax-error-expected.1039702/#post-6730855

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: https://discussions.unity.com/t/481379

Jesus Christ I have been trying to solve this for days now! THANK YOU!