我有一个带有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;
}
答案 0 :(得分:1)
你的硬币不应该与任何物体碰撞,尤其是玩家。
使硬币的对手触发,然后使用OnTriggerEnter2D
检测它何时接触到玩家以破坏硬币而不是OnCollisionEnter2D
。
附上你的硬币游戏对象:
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.CompareTag("Player"))
{
Debug.Log("Player detected. Destroying this coin");
Destroy(gameObject);
}
}
或附加到您的播放器GameObject:
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.CompareTag("Coin"))
{
Debug.Log("Coin detected. Destroying coin");
Destroy(collision.gameObject);
}
}