简体   繁体   English

unity RigidBody2D.AddForce() 方法停止玩家移动,卡住不动

[英]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.我正在尝试使用RigidBody2D.AddForce()方法移动我的玩家,但是每当我在任何方向上向玩家施加力时,玩家就会移动一段时间,然后立即卡在关卡(Tile)上的随机位置并且不会向前移动直到相反方向被按下。

I wanted to move player normally without immediately making velocity = 0 like experience.我想正常移动玩家,而不是像经验一样立即使速度 = 0。 It should follow slow decrease in acceleration as per force rule.根据力规则,它应该遵循加速度的缓慢下降。 I have checked my player movement on normal rigidbody without using Tilemap tiles.我在没有使用 Tilemap 瓷砖的情况下检查了我的玩家在正常刚体上的运动。 It's working fine but player stuck when using Tilemap tile level.它工作正常,但玩家在使用 Tilemap 平铺级别时卡住了。

I'm calling methods using Pointer Down and Pointer Up event system for my 100px x 100px sized sprites as shown in image.我正在为我的 100px x 100px 大小的精灵调用使用 Pointer Down 和 Pointer Up 事件系统的方法,如图所示。

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 级别:

Tilemap 调色板和组件设置检查器

My Output:我的 Output:

输出表示

If understand right:如果理解正确:

Your problem is caused by the fact that you are using a tilemap system made of squares colliders .您的问题由于您使用的是由 squares colliders 组成tilemap系统。 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.\我想您正在使用一个 object 来存储您的整个瓷砖地图。\

If it's the case:如果是这种情况:

  1. Go to the object your tilemap sit on. Go 到object您的tilemap所在的位置。
    Add component, Composite Collider 2D添加组件, Composite Collider 2D
    On your Tilemap Collider 2D check " Used by composite "您的Tilemap Collider 2D上检查“由复合材料使用

  2. Still in the object that contains your Tilemap : Go on the rigidbody and set it to static仍在包含您的Tilemap的 object 中:刚体上的Go并将其设置为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...现在(我希望)你的问题已经消失了......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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