XRoom_Unity/Assets/TwoHandScale.cs
2025-05-31 10:20:20 +03:30

65 lines
2.1 KiB
C#

using BNG;
using UnityEngine;
public class TwoHandScale : MonoBehaviour
{
public Grabbable grabbable;
public Transform hand1, hand2;
private float initialDistance;
private Vector3 initialScale;
public float initialParentScale;
public Vector3 worldHandScale = Vector3.one; // یا مثلاً (0.1f, 0.1f, 0.1f)
void Start()
{
if (grabbable == null)
grabbable = GetComponent<Grabbable>();
initialParentScale = transform.localScale.x;
}
void Update()
{
if (grabbable.BeingHeld && grabbable.HeldByGrabbers.Count == 2)
{
if (hand1 == null || hand2 == null)
{
hand1 = grabbable.HeldByGrabbers[0].transform;
hand2 = grabbable.HeldByGrabbers[1].transform;
initialDistance = Vector3.Distance(hand1.position, hand2.position);
initialScale = transform.localScale;
}
float currentDistance = Vector3.Distance(hand1.position, hand2.position);
float scaleFactor = currentDistance / initialDistance;
transform.localScale = initialScale * scaleFactor;
}
else
{
hand1 = null;
hand2 = null;
}
//if (hand1)
// SetWorldScale(hand1, worldHandScale);
//if (hand2)
// SetWorldScale(hand2, worldHandScale);
}
//void SetWorldScale(Transform obj, Vector3 targetWorldScale)
//{
// if (obj == null || obj.parent == null) return;
// Vector3 parentScale = obj.parent.lossyScale;
// // جلوگیری از تقسیم بر صفر
// parentScale.x = Mathf.Approximately(parentScale.x, 0) ? 0.0001f : parentScale.x;
// parentScale.y = Mathf.Approximately(parentScale.y, 0) ? 0.0001f : parentScale.y;
// parentScale.z = Mathf.Approximately(parentScale.z, 0) ? 0.0001f : parentScale.z;
// obj.localScale = new Vector3(
// targetWorldScale.x / parentScale.x,
// targetWorldScale.y / parentScale.y,
// targetWorldScale.z / parentScale.z
// );
//}
}