mirror of
https://github.com/nothke/quality-control.git
synced 2025-08-29 15:13:42 +00:00
Weight detector
This commit is contained in:
44
Assets/Scripts/WeightDetector.cs
Normal file
44
Assets/Scripts/WeightDetector.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
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());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user