hch
15 小时以前 af8dafd75f9a6fae017734d58fc7b34d6bdcd0f4
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
using UnityEngine;
using System.Collections;
using System;
 
public class SmoothMove : MonoBehaviour
{
 
    Vector3 targetPosition = Vector3.zero;
    float smoothTime = 0f;
    bool isLocal = false;
    Action callBack = null;
 
    Vector3 refPosition = Vector3.zero;
    bool startSmooth = false;
 
    public static float checkDistance = 0.01f;
 
    public static void MoveTo(GameObject _gameObject, Vector3 _targetPosition, float _smoothTime, bool _isLocal, Action _callBack)
    {
        SmoothMove smove = _gameObject.AddMissingComponent<SmoothMove>();
        smove.MoveTo(_targetPosition, _smoothTime, _isLocal, _callBack);
    }
 
    public void MoveTo(Vector3 _targetPosition, float _smoothTime, bool _isLocal, Action _callBack)
    {
 
        targetPosition = _targetPosition;
        smoothTime = _smoothTime;
        isLocal = _isLocal;
        callBack = _callBack;
 
        startSmooth = true;
    }
 
    private void LateUpdate()
    {
        if (!startSmooth)
        {
            return;
        }
 
        if (isLocal)
        {
            if (Vector3.Distance(this.transform.localPosition, targetPosition) > checkDistance)
            {
                Vector3 newPosition = Vector3.SmoothDamp(this.transform.localPosition, targetPosition, ref refPosition, smoothTime);
                this.transform.localPosition = newPosition;
            }
            else
            {
                this.transform.localPosition = targetPosition;
                if (callBack != null)
                {
                    callBack();
                    callBack = null;
                }
                Destroy(this);
            }
        }
        else
        {
            if (Vector3.Distance(this.transform.position, targetPosition) > 0.01f)
            {
                Vector3 newPosition = Vector3.SmoothDamp(this.transform.position, targetPosition, ref refPosition, smoothTime);
                this.transform.position = newPosition;
            }
            else
            {
                this.transform.position = targetPosition;
                if (callBack != null)
                {
                    callBack();
                    callBack = null;
                }
                Destroy(this);
            }
        }
 
    }
 
 
    void OnDisable()
    {
        if (callBack != null)
        {
            callBack();
            callBack = null;
        }
        startSmooth = false;
    }
}