wangguan
2020-11-12 a7956acb794f93fc01e16963efe1f38743b110f9
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using UnityEngine;
 
namespace ActionGameFramework.Projectiles
{
    /// <summary>
    /// A projectile that shoots upwards and then starts homing
    /// </summary>
    public class WobblingHomingProjectile : HomingLinearProjectile
    {
        protected enum State
        {
            Wobbling,
            Turning,
            Targeting
        }
 
        /// <summary>
        /// The time the projectile wobbles upward is randomized from this range
        /// </summary>
        public Vector2 wobbleTimeRange = new Vector2(1, 2);
 
        /// <summary>
        /// The number of wobble direction changes per second
        /// </summary>
        public float wobbleDirectionChangeSpeed = 4;
 
        /// <summary>
        /// The intensity of the wobble
        /// </summary>
        public float wobbleMagnitude = 7;
 
        /// <summary>
        /// The time the projectile takes to turn and home
        /// </summary>
        public float turningTime = 0.5f;
 
        /// <summary>
        /// State of projectile
        /// </summary>
        State m_State;
 
        /// <summary>
        /// Seconds wobbling
        /// </summary>
        protected float m_CurrentWobbleTime;
 
        /// <summary>
        /// Total time to wobble
        /// </summary>
        protected float m_WobbleDuration;
 
        /// <summary>
        /// Seconds turning to face homing target
        /// </summary>
        protected float m_CurrentTurnTime;
 
        /// <summary>
        /// Seconds for current turn
        /// </summary>
        protected float m_WobbleChangeTime;
 
        protected Vector3 m_WobbleVector,
                          m_TargetWobbleVector;
 
        protected override void Update()
        {
            // regular HomingLinearProjectile behaviour, handles a null homing target
            if (m_HomingTarget == null || m_State == State.Targeting)
            {
                base.Update();
                return;
            }
            switch (m_State)
            {
                // wobble the projectile
                case State.Wobbling:
                    m_CurrentWobbleTime += Time.deltaTime;
                    if (m_CurrentWobbleTime >= m_WobbleDuration)
                    {
                        m_State = State.Turning;
                        m_CurrentTurnTime = 0;
                    }
 
                    m_WobbleChangeTime += Time.deltaTime * wobbleDirectionChangeSpeed;
                    if (m_WobbleChangeTime >= 1)
                    {
                        m_WobbleChangeTime = 0;
                        m_TargetWobbleVector = new Vector3(Random.Range(-wobbleMagnitude, wobbleMagnitude),
                                                           Random.Range(-wobbleMagnitude, wobbleMagnitude), 0);
                        m_WobbleVector = Vector3.zero;
                    }
                    m_WobbleVector = Vector3.Lerp(m_WobbleVector, m_TargetWobbleVector, m_WobbleChangeTime);
                    m_Rigidbody.velocity = Quaternion.Euler(m_WobbleVector) * m_Rigidbody.velocity;
 
                    m_Rigidbody.rotation = Quaternion.LookRotation(m_Rigidbody.velocity);
                    break;
                // turn the projectile to face the homing target
                case State.Turning:
                    m_CurrentTurnTime += Time.deltaTime;
                    Quaternion aimDirection = Quaternion.LookRotation(GetHeading());
 
                    m_Rigidbody.rotation = Quaternion.Lerp(m_Rigidbody.rotation, aimDirection, m_CurrentTurnTime / turningTime);
                    m_Rigidbody.velocity = transform.forward * m_Rigidbody.velocity.magnitude;
 
                    if (m_CurrentTurnTime >= turningTime)
                    {
                        m_State = State.Targeting;
                    }
                    break;
            }
        }
 
        // select first wobble vector and set to wobble state
        protected override void Fire(Vector3 firingVector)
        {
            m_TargetWobbleVector = new Vector3(Random.Range(-wobbleMagnitude, wobbleMagnitude),
                                               Random.Range(-wobbleMagnitude, wobbleMagnitude), 0);
            m_WobbleDuration = Random.Range(wobbleTimeRange.x, wobbleTimeRange.y);
            base.Fire(firingVector);
            m_State = State.Wobbling;
            m_CurrentWobbleTime = 0.0f;
        }
    }
}