gpt4 book ai didi

c# - Rigidbody2D 在撞到对撞机时停止

转载 作者:行者123 更新时间:2023-11-30 17:31:15 25 4
gpt4 key购买 nike

我有一个带有 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;
}

最佳答案

您的硬币不应与任何物体碰撞,尤其是玩家。

让硬币的碰撞器被触发,然后使用 OnTriggerEnter2D 来检测它何时接触到玩家以销毁硬币而不是 OnCollisionEnter2D

enter image description here

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

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);
}
}

关于c# - Rigidbody2D 在撞到对撞机时停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48211990/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com