如何从Unity中的另一个脚本中获取对撞机被击中的次数
How to get the number of times the collider was hit from another script in Unity
我想用对撞机统计碰撞次数,请问以后怎么办?
detectflags.cs
public bool IsReceive=false;
public void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.CompareTag("MolotovCocktail"))
{
IsAttack.Value = true;
}
if (other.gameObject.CompareTag("Weapon")||other.gameObject.CompareTag("MolotovCocktail"))
{
IsReceive = true;
}
}
CountNumber.cs
public GameObject slimeChild;
private void GamaOverDecision()
{
if (slimeChild.GetComponent<ChildrenSlimeWeaponCollider>().IsReceive == true)
{
var SlimeCount = 0;
++SlimeCount;
if (SlimeCount == 5)
{
gameOverPopUp.GetComponent<GameOverPopUp>().SetView();
}
}
}
您可以在 Collision2D 对象上使用 GetContacts 方法,然后存储这些值,或者简单地存储来自方法调用的碰撞计数,这显然是简单的解决方案,尽管它可能不是最佳解决方案,但从你的其他评论来看,这似乎是你想要做的。
当我进入实际桌面时,稍后可能会使用更好的替代方案 + 代码示例更新此内容。
这里可以直接制作
public bool IsReceive=false;
public int slimCount;
public void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.CompareTag("MolotovCocktail"))
{
IsAttack.Value = true;
}
if (other.gameObject.CompareTag("Weapon")||other.gameObject.CompareTag("MolotovCocktail"))
{
IsReceive = true;
slimCount++
if(slimcount == 5){ doSomething();}
}
}
我想用对撞机统计碰撞次数,请问以后怎么办?
detectflags.cs
public bool IsReceive=false;
public void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.CompareTag("MolotovCocktail"))
{
IsAttack.Value = true;
}
if (other.gameObject.CompareTag("Weapon")||other.gameObject.CompareTag("MolotovCocktail"))
{
IsReceive = true;
}
}
CountNumber.cs
public GameObject slimeChild;
private void GamaOverDecision()
{
if (slimeChild.GetComponent<ChildrenSlimeWeaponCollider>().IsReceive == true)
{
var SlimeCount = 0;
++SlimeCount;
if (SlimeCount == 5)
{
gameOverPopUp.GetComponent<GameOverPopUp>().SetView();
}
}
}
您可以在 Collision2D 对象上使用 GetContacts 方法,然后存储这些值,或者简单地存储来自方法调用的碰撞计数,这显然是简单的解决方案,尽管它可能不是最佳解决方案,但从你的其他评论来看,这似乎是你想要做的。
当我进入实际桌面时,稍后可能会使用更好的替代方案 + 代码示例更新此内容。
这里可以直接制作
public bool IsReceive=false;
public int slimCount;
public void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.CompareTag("MolotovCocktail"))
{
IsAttack.Value = true;
}
if (other.gameObject.CompareTag("Weapon")||other.gameObject.CompareTag("MolotovCocktail"))
{
IsReceive = true;
slimCount++
if(slimcount == 5){ doSomething();}
}
}