View difference between Paste ID: K0Rhy8qy and CSLC3pUs
SHOW: | | - or go back to the newest paste.
1
//Titan optimization through toggling colliders based on hook raycasts, mouse raycasts, & trigger colliders
2
// - RiceCake & Josh
3
//Issues: Crosshair aim distance will not always work correctly on titans if grouped together
4
//Things to test for: Ghost hooks, clipping through titans (unlikely)
5
6
public class Bullet : Photon.MonoBehaviour
7
{
8
    //...
9
    public TITAN myTitan;
10
    
11
    private void OnDestroy()
12
    {
13
        //...
14
        //toggle titan once hook is destroyed
15
        if (this.myTitan != null)
16
        {
17
            this.myTitan.isHooked = false;
18
        }
19
        UnityEngine.Object.Destroy(this.rope);
20
    }
21
22
    //called in FixedUpdate under "else if (this.phase == 0)"
23
    public void checkTitan()
24
    {
25
        GameObject main = Camera.main.GetComponent<IN_GAME_MAIN_CAMERA>().main_object;
26
        if (main != null && this.master != null && this.master == main)
27
        {
28
            LayerMask mask = ((int)1) << LayerMask.NameToLayer("PlayerAttackBox");
29
            RaycastHit hit;
30
            if (Physics.Raycast(base.transform.position, this.velocity, out hit, 10f, mask.value))
31
            {
32
                Collider hitCollider = hit.collider;
33
                if (hitCollider.name.Contains("PlayerDetectorRC"))
34
                {
35
                    TITAN titan = hitCollider.transform.root.gameObject.GetComponent<TITAN>();
36
                    if (titan != null)
37
                    {
38
                        if (this.myTitan == null)
39
                        {
40
                            this.myTitan = titan;
41
                            this.myTitan.isHooked = true;
42
                        }
43
                        else if (this.myTitan != titan)
44
                        {
45
                            this.myTitan.isHooked = false;
46
                            this.myTitan = titan;
47
                            this.myTitan.isHooked = true;
48
                        }
49
                    }
50
                }
51
            }
52
        }
53
    }
54
}
55
56
public class HERO : Photon.MonoBehaviour
57
{
58
    //...
59
    public List<TITAN> myTitans;
60
61
    //called in showAimUI under "else"
62
    public void checkTitan()
63
    {
64
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
65
        LayerMask mask = ((int)1) << LayerMask.NameToLayer("PlayerAttackBox");
66
        LayerMask mask2 = ((int)1) << LayerMask.NameToLayer("Ground");
67
        LayerMask mask3 = ((int)1) << LayerMask.NameToLayer("EnemyBox");
68
        LayerMask mask4 = (mask | mask2) | mask3;
69
        RaycastHit[] hits = Physics.RaycastAll(ray, 180f, mask4.value);
70
        List<RaycastHit> sortedHits = new List<RaycastHit>();
71
        List<TITAN> currentTitans = new List<TITAN>();
72
        foreach (RaycastHit hit in hits)
73
        {
74
            sortedHits.Add(hit);
75
        }
76
        sortedHits.Sort((x,y) => x.distance.CompareTo(y.distance));
77
        float maxDistance = 180f;
78
        for (int i = 0; i < sortedHits.Count; i++)
79
        {
80
            GameObject hitObject = sortedHits[i].collider.gameObject;
81
            if (hitObject.layer == 16)
82
            {
83
                if (hitObject.name.Contains("PlayerDetectorRC") && sortedHits[i].distance < maxDistance)
84
                {
85
                    maxDistance -= 60f;
86
                    if (maxDistance <= 60f)
87
                    {
88
                        i = sortedHits.Count;
89
                    }
90
                    TITAN titan = hitObject.transform.root.gameObject.GetComponent<TITAN>();
91
                    if (titan != null)
92
                    {
93
                        currentTitans.Add(titan);
94
                    }
95
                }
96
            }
97
            else
98
            {
99
                i = sortedHits.Count;
100
            }
101
        }
102
        foreach (TITAN oldTitan in this.myTitans)
103
        {
104
            if (!currentTitans.Contains(oldTitan))
105
            {
106
                oldTitan.isLook = false;
107
            }
108
        }
109
        foreach (TITAN newTitan in currentTitans)
110
        {
111
            newTitan.isLook = true;
112
        }
113
        this.myTitans = currentTitans;
114
    }
115
}
116
117
public class TITAN : Photon.MonoBehaviour
118
{
119
    //...
120
    public bool colliderEnabled;
121
    public bool isHooked;
122
    public bool isLook;
123
    public TitanTrigger myTitanTrigger;
124
    public List<Collider> baseColliders;
125
126
    private void Start()
127
    {
128
	//...
129
130
        //cache colliders that will be toggled (all but AABB, the main movement capsule)
131
        this.baseColliders = new List<Collider>();
132
        
133
        foreach (Collider childCollider in base.GetComponentsInChildren<Collider>())
134
        {
135
            if (childCollider.name != "AABB")
136
            {
137
                this.baseColliders.Add(childCollider);
138
            }   
139
        }
140
141
        //create a new trigger collider to completely encompass the titan
142
        GameObject obj = new GameObject();
143
        obj.name = "PlayerDetectorRC";
144
        CapsuleCollider triggerCollider = obj.AddComponent<CapsuleCollider>();
145
        CapsuleCollider referenceCollider = base.transform.Find("AABB").GetComponent<CapsuleCollider>();
146
        triggerCollider.center = referenceCollider.center;
147
        triggerCollider.radius = Math.Abs(base.transform.Find("Amarture/Core/Controller_Body/hip/spine/chest/neck/head").position.y - base.transform.position.y);
148
        triggerCollider.height = referenceCollider.height * 1.2f;
149
        triggerCollider.material = referenceCollider.material;
150
        triggerCollider.isTrigger = true;
151
        triggerCollider.name = "PlayerDetectorRC";
152
        this.myTitanTrigger = obj.AddComponent<TitanTrigger>();
153
        this.myTitanTrigger.isCollide = false;
154
        obj.layer = 16;
155
        obj.transform.parent = base.transform.Find("AABB");
156
        obj.transform.localPosition = new Vector3(0f, 0f, 0f);
157
        this.colliderEnabled = true;
158
        this.isHooked = false;
159
        this.isLook = false;
160
    }
161
    
162
    //called in lateUpdate
163
    //compares hook & player trigger data to determine collider settings
164
    public void updateCollider()
165
    {
166
        if (this.colliderEnabled)
167
        {
168
            if (!this.isHooked && !this.myTitanTrigger.isCollide && !this.isLook)
169
            {
170
                foreach (Collider collider in this.baseColliders)
171
                {
172
                    collider.enabled = false;
173
                }
174
            this.colliderEnabled = false;
175
            }
176
        }
177
        else if (this.isHooked || this.myTitanTrigger.isCollide || this.isLook)
178
        {
179
            foreach (Collider collider in this.baseColliders)
180
            {
181
                collider.enabled = true;
182
            }
183
            this.colliderEnabled = true;
184
        }
185
    }
186
}
187
188
public class TitanTrigger : MonoBehaviour
189
{
190
    //detects if player is near enough to justify colliders
191
    public bool isCollide;
192
    private void OnTriggerEnter(Collider other)
193
    {
194
        GameObject obj = other.transform.root.gameObject;
195
        if (obj.layer == 8)
196
        {
197
            GameObject myPlayer = Camera.main.GetComponent<IN_GAME_MAIN_CAMERA>().main_object;
198
            if (myPlayer != null && myPlayer == obj)
199
            {
200
                this.isCollide = true;
201
            }
202
        }
203
    }
204
205
    private void OnTriggerExit(Collider other)
206
    {
207
        GameObject obj = other.transform.root.gameObject;
208
        if (obj.layer == 8)
209
        {
210
            GameObject myPlayer = Camera.main.GetComponent<IN_GAME_MAIN_CAMERA>().main_object;
211
            if (myPlayer != null && myPlayer == obj)
212
            {
213
                this.isCollide = false;
214
            }
215
        }
216
    }
217
}