Files
Studio/Assets/Scripts/XED/AssetTool/ObjectSnap.cs
2025-02-19 17:24:26 +09:00

54 lines
1.8 KiB
C#

using System.Collections;
using UnityEngine;
namespace XED.Util
{
public class ObjectSnap : MonoBehaviour
{
CustomAssetRenderObject parentObject;
private void Awake()
{
parentObject = GetComponentInParent<CustomAssetRenderObject>();
}
private void OnTriggerEnter(Collider other)
{
if (!Input.GetKey(KeyCode.V))
{
return;
}
ObjectSnap otherSnapPoint = other.gameObject.GetComponent<ObjectSnap>();
if (otherSnapPoint != null && otherSnapPoint.transform.parent != transform.parent)
{
StopAllCoroutines();
StartCoroutine(CoroutineCheckForSnap(otherSnapPoint));
}
}
IEnumerator CoroutineCheckForSnap(ObjectSnap otherSnapPoint)
{
Vector3 originPos = transform.position;
float originDist = Vector3.Magnitude(otherSnapPoint.transform.position - originPos);
float currDist = 0.0f;
Vector3 distance = Vector3.zero;
yield return new WaitForSeconds(0.1f);
if (originPos == transform.position)
{
yield break;
}
distance = otherSnapPoint.transform.position - transform.position;
currDist = distance.magnitude;
if (currDist > originDist * 1.2f)
{
yield break;
}
float snapTime = 0.0f;
while (snapTime < 1.5f)
{
distance = otherSnapPoint.transform.position - transform.position;
parentObject.transform.position = parentObject.transform.position + distance;
snapTime += Time.deltaTime;
yield return null;
}
}
}
}