如果武器与身体碰撞会造成伤害,如果武器与其他武器碰撞则不会造成伤害。
问题是,当两个武器碰撞时,它会检测到玩家对象和哥布林对象碰撞,而不是实际的武器碰撞。
我已经尝试过:
public class ObjectDamage : MonoBehaviour
{
public int damage;
public int team;
public float bounceForce = 5f;
void Start()
{
}
void Update()
{
}
private void OnCollisionEnter2D(Collision2D collision)
{
Debug.Log($"Collided with object with tag: {collision.gameObject.tag}");
if (collision.gameObject.layer == 3)
{
ClashWithOtherWeapon();
Debug.Log("Reached!");
return;
}
ObjectHealth objectHealth = collision.gameObject.GetComponent<ObjectHealth>();
if (objectHealth != null && objectHealth.team != team)
{
objectHealth.TakeDamage(damage);
BounceEnemyBack(collision);
}
}
private void ClashWithOtherWeapon()
{
return;
}
private void BounceEnemyBack(Collision2D collision)
{
Rigidbody2D rb = collision.gameObject.GetComponent<Rigidbody2D>();
if (rb != null)
{
Vector2 bounceDirection = collision.transform.position - transform.position;
bounceDirection.Normalize();
rb.AddForce(bounceDirection * bounceForce, ForceMode2D.Impulse);
}
}
}
您应该检查
collision.collider
,它代表与当前对象碰撞的Collider
组件。
collision.gameObject
指的是与碰撞器关联的 Rigidbody
所附着的游戏对象。