54 lines
1.8 KiB
C#
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;
|
|
}
|
|
}
|
|
}
|
|
}
|