first commit

This commit is contained in:
2026-04-07 03:14:32 +03:00
commit b3992fec6b
1026 changed files with 366769 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
using UnityEngine;
namespace JusticeScale.Scripts
{
[RequireComponent(typeof(ScaleController))]
public class ScaleBeamRotation : MonoBehaviour
{
[SerializeField] [Tooltip("Reference to the ScaleController that manages the balance logic.")]
private ScaleController scaleController;
[SerializeField]
[Tooltip("The Transform representing the balance beam that rotates based on the weight difference.")]
private Transform balanceBeam;
[SerializeField]
[Range(0, 75)]
[Tooltip(
"Maximum rotation angle of the balance beam in degrees. The beam will rotate between -blendRotation and +blendRotation. " +
"It's recommended to test different values in the editor to see how the beam rotates visually.")]
private float blendRotation = 15;
private void Awake()
{
// Automatically get the ScaleController component attached to the same GameObject
if (scaleController == null) scaleController = GetComponent<ScaleController>();
}
private void FixedUpdate()
{
// Calculates the rotation based on the normalized balance value.
// If BalanceNormalized is 0, the beam is fully tilted to the left (-blendRotation).
// If BalanceNormalized is 1, the beam is fully tilted to the right (+blendRotation).
// If BalanceNormalized is 0.5, the beam is perfectly balanced (rotation = 0).
var rotation = Mathf.Lerp(-blendRotation, blendRotation, scaleController.BalanceNormalized);
// Apply the calculated rotation to the balance beam's local rotation.
balanceBeam.localRotation = Quaternion.Euler(0, 0, rotation);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 57bc8f46e7805e1498eca027f3419f66
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,55 @@
using JusticeScale.Scripts.Scales;
using UnityEngine;
namespace JusticeScale.Scripts
{
public class ScaleController : MonoBehaviour
{
[Header("Scales")]
[SerializeField]
[Tooltip("Reference to the left scale. Should be assigned to the left side of the balance.")]
private Scale leftScale;
[SerializeField]
[Tooltip("Reference to the right scale. Should be assigned to the right side of the balance.")]
private Scale rightScale;
[Tooltip("Normalized value representing the current balance. 0 means fully tilted to the left, 1 means fully tilted to the right, and 0.5 is perfectly balanced.")]
public float BalanceNormalized { get; private set; } = 0.5f;
[Tooltip("The difference in weight between the left and right scales. A positive value means the left scale is heavier, while a negative value means the right scale is heavier.")]
public float WeightDifference { get; private set; }
[Header("Configuration")]
[Min(0.01f)]
[Tooltip("The maximum weight difference between the two sides for the scale to reach its limit. A higher value makes the balance less sensitive.")]
public float maxWeightDifference = 10f;
[SerializeField]
[Tooltip("The smoothing time for updating the balance. This helps to avoid abrupt changes that could cause erratic behavior or glitches in the balance's operation.")]
private float balanceSmoothTime = 0.05f;
[SerializeField] [Tooltip("Internal smoothed result for the balance. Used for gradual balance adjustments.")]
private float weightResultSmoothed;
private void Update()
{
if (!leftScale || !rightScale)
{
Debug.LogWarning("The scale references are not assigned.");
return;
}
UpdateBalance();
}
private void UpdateBalance()
{
var targetWeightDifference = leftScale.TotalWeight - rightScale.TotalWeight;
WeightDifference = targetWeightDifference;
weightResultSmoothed = Mathf.Lerp(weightResultSmoothed, targetWeightDifference, balanceSmoothTime);
BalanceNormalized = Mathf.InverseLerp(-maxWeightDifference, maxWeightDifference, weightResultSmoothed);
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 77cc71e5d9b8ba643813860e7cf4b299
timeCreated: 1719307373

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 02bd81f2c0c1782429beed52556f5e7b
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,122 @@
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace JusticeScale.Scripts.Scales
{
public class OverlapScale : Scale
{
[Header("Capsule Config")]
[Min(0)] [SerializeField]
private float capsuleLength = 1.5f; // The vertical length of the capsule used to detect objects above the scale
[Min(0)] [SerializeField]
private float capsuleRadius = 0.5f; // The radius of the capsule, determining its width
[Min(0)] [SerializeField]
private float capsuleOffsetHeight = 0.5f; // The vertical offset from the scales position to the start of the capsule
private Vector3 _startPoint, _endPoint; // The calculated start and end points of the capsule
private readonly HashSet<Rigidbody> _detectedObjects = new(); // Set of detected rigidbodies to avoid duplicates
private readonly List<Rigidbody> _objectsInContainer = new(); // List of objects currently inside the capsule area
private GameObject _objectContainer; // A container object to parent detected objects
private float _previousWeight; // Stores the previous total weight to optimize calculations
// Returns the calculated total weight of objects detected inside the capsule
public override float TotalWeight => CalculateWeightInCapsule();
private float CalculateWeightInCapsule()
{
weight = 0f;
_detectedObjects.Clear();
// Adjust the capsule variables by the scale of the GameObject
var scaledCapsuleLength = capsuleLength * transform.lossyScale.normalized.magnitude;
var scaledCapsuleRadius = capsuleRadius * transform.lossyScale.normalized.magnitude;
var scaledCapsuleOffsetHeight = capsuleOffsetHeight * transform.lossyScale.normalized.magnitude;
// Define capsule start and end points based on configuration, adjusted for the scale
_startPoint = transform.position + Vector3.up * scaledCapsuleOffsetHeight;
_endPoint = _startPoint + Vector3.up * scaledCapsuleLength;
// Perform the OverlapCapsule detection, gathering all colliders within the volume
// ReSharper disable once Unity.PreferNonAllocApi
Collider[] colliders = Physics.OverlapCapsule(_startPoint, _endPoint, scaledCapsuleRadius, layerMask);
foreach (var collider in colliders)
{
var rb = collider.GetComponent<Rigidbody>();
if (rb != null && !_detectedObjects.Contains(rb))
{
_detectedObjects.Add(rb);
ManageObjectContainer(rb);
weight += rb.mass;
if (!_objectsInContainer.Contains(rb)) _objectsInContainer.Add(rb);
}
}
RemoveDetectedObjectsNotInCapsule(_detectedObjects);
// Exit early if the weight hasn't changed significantly
if (Mathf.Abs(weight - _previousWeight) < 0.001f) return _previousWeight;
_previousWeight = weight;
return weight;
}
private void ManageObjectContainer(Rigidbody rb)
{
if (_objectContainer == null)
{
_objectContainer = new GameObject("Objects Container");
_objectContainer.transform.parent = transform;
}
rb.transform.SetParent(_objectContainer.transform, true);
}
private void RemoveDetectedObjectsNotInCapsule(HashSet<Rigidbody> detectedObjects)
{
foreach (var obj in _objectsInContainer.ToList())
if (!detectedObjects.Contains(obj))
{
obj.transform.parent = null;
_objectsInContainer.Remove(obj);
}
if (detectedObjects.Count == 0) Destroy(_objectContainer);
}
private void OnDrawGizmosSelected()
{
if (transform == null) return;
// Adjust the capsule variables by the scale of the GameObject
float scaledCapsuleLength = capsuleLength * 0.1f * transform.lossyScale.magnitude;
float scaledCapsuleRadius = capsuleRadius * 0.1f * transform.lossyScale.magnitude;
float scaledCapsuleOffsetHeight = capsuleOffsetHeight * 0.1f * transform.lossyScale.magnitude;
// Define the start and end points, adjusted for the object's scale
_startPoint = transform.position + Vector3.up * scaledCapsuleOffsetHeight;
_endPoint = _startPoint + Vector3.up * scaledCapsuleLength;
// Draw the capsule in the editor (with scaling applied)
Gizmos.color = Color.blue;
Gizmos.DrawWireSphere(_startPoint, scaledCapsuleRadius);
Gizmos.DrawWireSphere(_endPoint, scaledCapsuleRadius);
Gizmos.DrawLine(
_startPoint + Vector3.up * scaledCapsuleRadius,
_endPoint + Vector3.up * scaledCapsuleRadius
);
Gizmos.DrawLine(
_startPoint - Vector3.up * scaledCapsuleRadius,
_endPoint - Vector3.up * scaledCapsuleRadius
);
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 8b64e6584f3a49a5b30287827ab3f516
timeCreated: 1731300775

View File

@@ -0,0 +1,18 @@
using UnityEngine;
namespace JusticeScale.Scripts.Scales
{
public abstract class Scale : MonoBehaviour
{
public abstract float TotalWeight { get; }
[SerializeField] protected LayerMask layerMask = -1; // LayerMask specifying which layers to include in the scale detection
[SerializeField] [Tooltip("The current weight of the scale (not visible in the Inspector), for easy developer testing.")]
protected float weight; // Current weight for inspector display; TotalWeight is the actual calculated value.
private void Update()
{
weight = TotalWeight;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 3a958ddeab399d44eb4e129be0ea1cc8
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,80 @@
using System.Collections.Generic;
using UnityEngine;
namespace JusticeScale.Scripts.Scales
{
[RequireComponent(typeof(MeshCollider))]
public class TriggerScale : Scale
{
[Tooltip("The total weight of the objects currently on this scale trigger.")]
public override float TotalWeight => weight;
// Container for objects detected by the scale
private GameObject _objectContainer;
// HashSet to track objects on the scale, ensuring each is only counted once
// (useful if the GameObject has multiple triggers that could detect the same object).
private HashSet<Transform> _detectedObjects = new HashSet<Transform>();
private void Start()
{
// Initialize the object container at the start
_objectContainer = new GameObject("Objects Container");
_objectContainer.transform.parent = transform;
}
private void OnTriggerEnter(Collider other)
{
var rb = other.GetComponent<Rigidbody>();
if (rb != null && IsInDetectableLayer(other.gameObject) && _detectedObjects.Add(rb.transform))
{
AddObjectToContainer(rb.transform);
weight += rb.mass;
// Round the total weight to 2 decimal places
weight = Mathf.Round(weight * 100f) / 100f;
}
}
private void OnTriggerExit(Collider other)
{
var rb = other.GetComponent<Rigidbody>();
if (rb != null && _detectedObjects.Remove(rb.transform))
{
weight -= rb.mass;
// Ensure total weight doesn't drop below zero
weight = Mathf.Max(0, weight);
weight = Mathf.Round(weight * 100f) / 100f;
RemoveObjectFromContainer(rb.transform);
}
}
private void AddObjectToContainer(Transform objectTransform)
{
if (_objectContainer == null)
{
// Create a new container for the objects if it doesn't exist
_objectContainer = new GameObject("Objects Container");
_objectContainer.transform.parent = transform;
}
// Set the parent of the object to the container
objectTransform.SetParent(_objectContainer.transform, true);
}
private void RemoveObjectFromContainer(Transform objectTransform)
{
// Unparent the object from the container
if (_objectContainer != null) objectTransform.parent = null;
// Destroy the container if it has no child objects left
if (_objectContainer.transform.childCount == 0) Destroy(_objectContainer);
}
private bool IsInDetectableLayer(GameObject obj)
{
// Check if the object is in the correct layer
return ((layerMask.value & (1 << obj.layer)) != 0);
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 76c938acc67ae6746ad167809e6b86e8
timeCreated: 1728533272

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: e12ae2f22708f164e9a713227f37bbc0
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,38 @@
using System;
using JusticeScale.Scripts.Scales;
using UnityEngine;
using UnityEngine.UI;
namespace JusticeScale.Scripts.UI
{
public class ScaleUI : MonoBehaviour
{
[SerializeField] private Scale scale; // Reference to the Scale component
[SerializeField] private Text balanceText;
[SerializeField] private bool isInPounds;
private void Awake()
{
// Automatically get the TriggerScale component attached to the parent GameObject
if (scale == null) scale = GetComponentInParent<TriggerScale>();
// Automatically get the Text component attached to the child GameObject
if (balanceText == null) balanceText = GetComponentInChildren<Text>();
}
private void Update()
{
// Update the balance text based on the selected unit
balanceText.text = isInPounds
? $"{ConvertKgToPound(scale.TotalWeight)} pounds"
: $"{scale.TotalWeight} kg";
}
private float ConvertKgToPound(float massKg)
{
var poundConverse = massKg * 2.20f;
return (float)Math.Round(poundConverse, 2); // Round to 2 decimal places
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f638d3b30aee7c34286179635223e13c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: