【问题标题】:Rigidbody2D stops when hits colliderRigidbody2D 遇到碰撞体时停止
【发布时间】:2018-01-11 16:41:24
【问题描述】:

我有一个带有 Rigidbody2D 和圆形对撞机的玩家。玩家收集具有圆形对撞机的硬币。我左右移动播放器。当它收集硬币时,硬币会破坏并且玩家停止,所以我必须再次触摸才能继续向左/向右移动。那么,如何在不停止玩家的情况下收集硬币呢?这是我移动刚体的代码:

void TouchMove()
{
    if (Input.touchCount > 0)
    {
        Touch touch = Input.GetTouch(0);
        float middle = Screen.width / 2;

        if (touch.position.x < middle && touch.phase == TouchPhase.Began)
        {
            MoveLeft();
        }

        else if (touch.position.x > middle && touch.phase == TouchPhase.Began)
        {
            MoveRight();
        }
    }
    else
    {
        SetVelocityZero();
    }
}

public void MoveLeft()
{
    rb.velocity = new Vector2(-playerSpeed, 0);
}

public void MoveRight()
{
    rb.velocity = new Vector2(playerSpeed, 0);
}

public void SetVelocityZero()
{
    rb.velocity = Vector2.zero;
}

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    您的硬币不应与任何物体(尤其是玩家)发生碰撞。

    使硬币的对撞机被触发,然后使用OnTriggerEnter2D检测它何时接触到玩家以摧毁硬币而不是OnCollisionEnter2D。

    附加到您的硬币游戏对象:

    void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.CompareTag("Player"))
        {
            Debug.Log("Player detected. Destroying this coin");
            Destroy(gameObject);
        }
    }
    

    或附加到您的玩家游戏对象:

    void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.CompareTag("Coin"))
        {
            Debug.Log("Coin detected. Destroying coin");
            Destroy(collision.gameObject);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多