From f20994a8dbcadc8404e4b9e0263f1d7d501388bf Mon Sep 17 00:00:00 2001 From: nothke Date: Sat, 17 Aug 2024 11:55:20 +0200 Subject: [PATCH] Weight detector --- Assets/Audio.meta | 8 +++++ Assets/Scenes/test_conveyor_belt.unity | 26 ++++++++++++++- Assets/Scripts/WeightDetector.cs | 44 ++++++++++++++++++++++++++ Assets/Scripts/WeightDetector.cs.meta | 11 +++++++ 4 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 Assets/Audio.meta create mode 100644 Assets/Scripts/WeightDetector.cs create mode 100644 Assets/Scripts/WeightDetector.cs.meta diff --git a/Assets/Audio.meta b/Assets/Audio.meta new file mode 100644 index 0000000..2952005 --- /dev/null +++ b/Assets/Audio.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: ce453247b3c5cf14c80ea8ec288530d9 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scenes/test_conveyor_belt.unity b/Assets/Scenes/test_conveyor_belt.unity index b1d2c0d..178cc81 100644 --- a/Assets/Scenes/test_conveyor_belt.unity +++ b/Assets/Scenes/test_conveyor_belt.unity @@ -615,6 +615,23 @@ PrefabInstance: m_AddedGameObjects: [] m_AddedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: 79b1ab56bfd061d46a689142c3992685, type: 3} +--- !u!1 &891192242 stripped +GameObject: + m_CorrespondingSourceObject: {fileID: 2145288034412905379, guid: 0fb5ec34d66e2824c92ed0b720ca13df, type: 3} + m_PrefabInstance: {fileID: 6217297023818049519} + m_PrefabAsset: {fileID: 0} +--- !u!114 &891192249 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 891192242} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: f935fa08cd417994097c7b59d2547f0d, type: 3} + m_Name: + m_EditorClassIdentifier: --- !u!1001 &1075122282 PrefabInstance: m_ObjectHideFlags: 0 @@ -1333,6 +1350,10 @@ PrefabInstance: serializedVersion: 3 m_TransformParent: {fileID: 0} m_Modifications: + - target: {fileID: 6230516344177671089, guid: 79b1ab56bfd061d46a689142c3992685, type: 3} + propertyPath: m_Mass + value: 0.5 + objectReference: {fileID: 0} - target: {fileID: 7217391875016474312, guid: 79b1ab56bfd061d46a689142c3992685, type: 3} propertyPath: m_LocalPosition.x value: 22.29 @@ -1555,7 +1576,10 @@ PrefabInstance: m_RemovedComponents: [] m_RemovedGameObjects: [] m_AddedGameObjects: [] - m_AddedComponents: [] + m_AddedComponents: + - targetCorrespondingSourceObject: {fileID: 2145288034412905379, guid: 0fb5ec34d66e2824c92ed0b720ca13df, type: 3} + insertIndex: -1 + addedObject: {fileID: 891192249} m_SourcePrefab: {fileID: 100100000, guid: 0fb5ec34d66e2824c92ed0b720ca13df, type: 3} --- !u!1001 &6783293368001247804 PrefabInstance: diff --git a/Assets/Scripts/WeightDetector.cs b/Assets/Scripts/WeightDetector.cs new file mode 100644 index 0000000..bb1a178 --- /dev/null +++ b/Assets/Scripts/WeightDetector.cs @@ -0,0 +1,44 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class WeightDetector : MonoBehaviour +{ + HashSet enteredBodies = new HashSet(); + + 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()); + } +} diff --git a/Assets/Scripts/WeightDetector.cs.meta b/Assets/Scripts/WeightDetector.cs.meta new file mode 100644 index 0000000..9774895 --- /dev/null +++ b/Assets/Scripts/WeightDetector.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: f935fa08cd417994097c7b59d2547f0d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: