Files
ScienceLab.WeightAndMass/Assets/Scripts/ScaleZone.cs
2026-04-07 03:14:32 +03:00

44 lines
913 B
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScaleZone : MonoBehaviour
{
private List<Rigidbody> _objects = new List<Rigidbody>();
private void OnTriggerEnter(Collider other)
{
Rigidbody rb = other.GetComponentInParent<Rigidbody>();
if (rb == null) return;
if (!rb.CompareTag("Target")) return;
if (!_objects.Contains(rb))
_objects.Add(rb);
}
private void OnTriggerExit(Collider other)
{
Rigidbody rb = other.GetComponentInParent<Rigidbody>();
if (rb == null) return;
_objects.Remove(rb);
}
public float TotalWeight
{
get
{
float total = 0f;
foreach (var rb in _objects)
{
if (rb != null)
total += rb.mass;
}
return total;
}
}
}