Home > OS >  Unity RigidBody2D.AddForce() method stopped player movement, stuck without move
Unity RigidBody2D.AddForce() method stopped player movement, stuck without move

Time:08-14

I'm trying to move my player using RigidBody2D.AddForce() method but whenever and i apply force to player on any direction then player moves for sometime then immediately stuck at random locations on level (Tile) and doesn't move ahead until opposite direction is pressed.

I wanted to move player normally without immediately making velocity = 0 like experience. It should follow slow decrease in acceleration as per force rule. I have checked my player movement on normal rigidbody without using Tilemap tiles. It's working fine but player stuck when using Tilemap tile level.

I'm calling methods using Pointer Down and Pointer Up event system for my 100px x 100px sized sprites as shown in image.

My Code:

public class PlayerScript : MonoBehaviour
{
    Rigidbody2D playerRigidBody;
    float speed=15f;
    float forceX,forceY;
    float maxVeloX,maxVeloY;
    bool isMoveLeft,isMoveRight,isMoveUp;
    
    void Start()
    {
        playerRigidBody=GetComponent<Rigidbody2D>();
        isMoveLeft=isMoveRight=isMoveUp=false;
    }

    void Update()
    {
        forceX=forceY=0f;
        maxVeloX=Mathf.Abs(playerRigidBody.velocity.x);
        maxVeloY=Mathf.Abs(playerRigidBody.velocity.y);

        if(isMoveRight) {
            if(maxVeloX<6){
                forceX= speed;
                transform.localScale=new Vector3(1,1,1); // to face player right direction
            }
        }else if(isMoveLeft ){
            if(maxVeloX<6){
                forceX= -speed;
                transform.localScale=new Vector3(-1,1,1); // to face left direction
            }   
        }
        else if(isMoveUp )
                forceY=25f;   

        playerRigidBody.AddForce(new Vector2(forceX,forceY));
    }

    public void MoveLeftStart(){ // button left press
        
        isMoveLeft=true;
    }
    public void MoveLeftEnd(){ // button left release
        
        isMoveLeft=false;
    }
    public void MoveRightStart(){
        
        isMoveRight=true;
    }
    public void MoveRightEnd(){
        isMoveRight=false;
    }

    public void MoveUpStart(){
        isMoveUp=true;
    }
    public void MoveUpEnd(){
        isMoveUp=false;
    }
}

My Tilemap level:

Tilemap pallete and component settings inspector

CodePudding user response:

If understand right:

Your problem is caused by the fact that you are using a tilemap system made of squares colliders. And your Rigidbody is always pushed to the ground due to gravity. So everytime you collide with a corner of these colliders your character is stuck.

The red spikes represent every position your character might get stuck

I suppose you are using one object to store your whole tilemap.\

If it's the case:

  1. Go to the object your tilemap sit on.
    Add component, Composite Collider 2D
    On your Tilemap Collider 2D check "Used by composite"

  2. Still in the object that contains your Tilemap: Go on the rigidbody and set it to static

The composite collider will remove all those separate squares and regroup them.
It will remove all those little spikes.

And now (i hope) your problem is gone...

  • Related