简体   繁体   中英

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

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 调色板和组件设置检查器

My Output:

输出表示

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.

红色尖刺代表你的角色可能卡住的每个位置

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM