86 lines
2.2 KiB
C#
86 lines
2.2 KiB
C#
using UnityEngine;
|
|
using System.Collections;
|
|
using Vuforia;
|
|
|
|
public enum AtomType
|
|
{
|
|
H, O, C, N, S, Na, Cl
|
|
}
|
|
|
|
public class Atom : MonoBehaviour
|
|
{
|
|
public AtomType atomType;
|
|
public GameObject visual;
|
|
|
|
[HideInInspector] public bool isTracked;
|
|
|
|
ObserverBehaviour observer;
|
|
Coroutine popCoroutine;
|
|
|
|
void Awake()
|
|
{
|
|
observer = GetComponent<ObserverBehaviour>();
|
|
}
|
|
|
|
void OnEnable()
|
|
{
|
|
if (observer != null)
|
|
observer.OnTargetStatusChanged += OnStatusChanged;
|
|
}
|
|
|
|
void OnDisable()
|
|
{
|
|
if (observer != null)
|
|
observer.OnTargetStatusChanged -= OnStatusChanged;
|
|
}
|
|
|
|
void OnStatusChanged(ObserverBehaviour behaviour, TargetStatus status)
|
|
{
|
|
bool newStatus = status.Status == Status.TRACKED;
|
|
|
|
if (newStatus && !isTracked)
|
|
{
|
|
isTracked = true;
|
|
if (visual != null)
|
|
{
|
|
visual.SetActive(true);
|
|
if (popCoroutine != null) StopCoroutine(popCoroutine);
|
|
popCoroutine = StartCoroutine(AnimatePop(visual.transform));
|
|
}
|
|
}
|
|
else if (!newStatus && isTracked)
|
|
{
|
|
isTracked = false;
|
|
if (visual != null) visual.SetActive(false);
|
|
}
|
|
}
|
|
|
|
// --- ANIMASI EASE OUT BACK (PREMIUM FEEL) ---
|
|
IEnumerator AnimatePop(Transform target)
|
|
{
|
|
float timer = 0;
|
|
float duration = 0.6f; // Sedikit dipelanin biar curve-nya kerasa
|
|
|
|
target.localScale = Vector3.zero;
|
|
|
|
while (timer < duration)
|
|
{
|
|
timer += Time.deltaTime;
|
|
float x = timer / duration; // Nilai 0 sampai 1
|
|
|
|
// RUMUS EASE OUT BACK
|
|
// c1 adalah seberapa jauh dia "kebablasan" (overshoot).
|
|
// 1.70158 adalah standar industri. Kalau mau lebih "mentul", naikkan jadi 2.5f.
|
|
float c1 = 1.70158f;
|
|
float c3 = c1 + 1;
|
|
|
|
// Rumus Matematika: 1 + c3 * (x-1)^3 + c1 * (x-1)^2
|
|
float scale = 1 + c3 * Mathf.Pow(x - 1, 3) + c1 * Mathf.Pow(x - 1, 2);
|
|
|
|
target.localScale = Vector3.one * scale;
|
|
yield return null;
|
|
}
|
|
|
|
target.localScale = Vector3.one;
|
|
}
|
|
} |