quality-control/Assets/Scripts/WeightDetector.cs

45 lines
838 B
C#
Raw Permalink Normal View History

2024-08-17 09:55:20 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WeightDetector : MonoBehaviour
{
HashSet<Rigidbody> enteredBodies = new HashSet<Rigidbody>();
private void OnCollisionEnter(Collision collision)
{
var rb = collision.rigidbody;
if (rb)
{
enteredBodies.Add(rb);
}
}
private void OnCollisionExit(Collision collision)
{
var rb = collision.rigidbody;
if (rb)
{
enteredBodies.Remove(rb);
}
}
public float GetTotalWeight()
{
float totalMass = 0;
foreach (var body in enteredBodies)
{
totalMass += body.mass;
}
return totalMass;
}
//private void Update()
//{
// Debug.Log(GetTotalWeight());
//}
2024-08-17 09:55:20 +00:00
}