using System.Collections; using System.Collections.Generic; using UnityEngine; using System; public class CircleMoveTo : MonoBehaviour { public float circleTime = 1f; public float duration = 2f; public float ratio = 3; public float accelerate = 0.2f; [SerializeField] Vector3 m_Desitination = Vector3.zero; public Vector3 destination { get { return m_Desitination; } private set { m_Desitination = value; } } Vector3 forward = Vector3.zero; float timer = 0f; float endTime = 0f; float velocity = 0f; bool windUp = false; Vector3 refPosition = Vector3.zero; Action onReach; bool beginMove = false; public void MoveTo(Vector3 _destination, Action _callBack = null) { this.SetActive(true); windUp = false; velocity = 0f; destination = _destination; beginMove = true; endTime = Time.time + duration; refPosition = Vector3.zero; onReach = _callBack; } void FixedUpdate() { if (!beginMove) { return; } if (Time.time > endTime && !windUp) { var normalize1 = Vector3.Normalize(destination.SetZ(0)); var normalize2 = Vector3.Normalize(this.transform.position.SetZ(0)); var dot = Vector3.Dot(normalize1, normalize1 - normalize2); if (dot > -0.15f && dot < 0.15f) { windUp = true; } } if (windUp) { var newPosition = Vector3.SmoothDamp(this.transform.position, destination, ref refPosition, 0.25f); this.transform.position = newPosition; this.transform.localPosition = this.transform.localPosition.SetZ(0); if (Vector3.Distance(this.transform.position.SetZ(0), destination.SetZ(0)) < 0.01f) { if (onReach != null) { onReach(); onReach = null; } beginMove = false; this.SetActive(false); } } else { velocity += Time.deltaTime * accelerate; timer += Time.deltaTime; if (timer > circleTime) { timer = timer - circleTime; } var t = Mathf.Clamp01(timer / circleTime); forward = new Vector3(Mathf.Sin(2 * t * Mathf.PI), Mathf.Cos(2 * t * Mathf.PI), 0f); this.transform.position += Vector3.Normalize(forward) * Mathf.Pow(velocity, ratio); } } }