c# - 如何从 Unity 中的另一个脚本中获取对撞机被击中的次数
问题描述
我想计算与碰撞器的碰撞次数,从现在开始我该怎么做?
检测标志.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 方法,然后存储这些值,或者只是存储方法调用中的碰撞计数,这显然是简单的解决方案,虽然它可能不是最好的解决方案,但来自您的其他评论这似乎是你想要做的。
当我进入实际桌面时,可能会在稍后使用更好的替代方案 + 代码示例来更新它。
推荐阅读
- php - 从 .js 文件将数组发布到 PHP 页面
- amazon-web-services - 我可以在一个 AWS 数据库集群中创建多个数据库实例吗?
- javascript - 如何包装来自第三方 api 的错误?
- mysql - 查询MySQL中至少一个孩子满足约束1且所有孩子满足约束2的所有行
- python - Python:使用 groupby 获取组中值最接近 5 的行
- spring-security-oauth2 - OAuth 错误:无效的客户端和错误的错误客户端凭据
- python - 如何用 Python(Sympy) 实现一个函数,实现与 Wolfram Mathematica 中的 ToExpression 相同?
- php - Laravel/Lighthouse - 解析为外部 API
- angular - 将文件复制到网络中的远程服务器
- react-native - React Native Navigation 覆盖 onBack