少年修仙传客户端代码仓库
hch
2 天以前 600733c8f592cb9e65f2b7a3e110ac1d686e6bfe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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);
        }
 
    }
 
 
}