/
PlayerController.cs
710 lines (625 loc) · 25.2 KB
/
PlayerController.cs
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
using System.Collections;
using System.Collections.Generic;
using XInputDotNetPure;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
Rigidbody2D rb;
public bool canReuseCabinets = false;
public float playerSpeed = 3;
public float playerReverseSpeed = 4;
public float DeadZone = 0f;
Vector3 playerSpawnPos;
GameObject attachedCable;
public bool isSpaceShip;
public LineRenderer CableLineRendered;
public float CableMinDistance = 0.25f;
float CableNextDistance = 0.5f;
public float CableMaxLength = 15f;
// Do not change
public float CableCurrentLenght = 0f;
public float CableCurrentLenghtFromCable0 = 0f;
public float CableVibrationStartFromEnd = 10;
public float CableSlowDownStartFromEnd = 2;
public float CablePullingSlowDownStartFromLast0 = 4f;
public float VibrationIntensity = 0.4f;
public bool hasCable = false;
public bool isActive = false;
public DayNightCycle DayNightCycle;
public Animator anim;
public SpriteRenderer spriteRendered;
PlayerDeathController deathController;
public float BashingPower = 14f;
public float BashingAmount = 0f;
public float BashingPowerMin = 0.5f;
float DisableMovementTimer = 0f;
public float LastVibrationAlpha = 0f;
public bool CanEatLastCablePosition = false;
public float ClosedEyesTime = 0.3f;
public float OpenedEyesTime = 0.8f;
bool eyesOpened = true;
// Do not change
public float ClosedEyesTimer = 0f;
public float CablePickupDistance = 0.301f;
int Cable0 = 0;
Vector2 CablePickupPosition = Vector2.zero;
LevelStart levelStart;
public GameObject CablePickup;
public GameObject CablePickupInstance;
public enum Gamepads
{
GAMEPAD_1,
GAMEPAD_2,
GAMEPAD_3,
GAMEPAD_4
}
public Gamepads gamepadAssigned;
public enum AnimationDirections
{
FACING_UP,
FACING_DOWN,
FACING_LEFT,
FACING_RIGHT
}
AnimationDirections animDir;
public bool GameIsThrowning = true;
public float ThrowMultiplier = 10f;
public bool ChargingThrow = false;
public float ThrowPower = 0f;
public float MaxThrowPower = 3f;
public Vector2 ThrowDirection;
Transform CableTarget;
void Start()
{
CableTarget = transform;
levelStart = FindObjectOfType<LevelStart>();
DayNightCycle = FindObjectOfType<DayNightCycle>();
rb = GetComponent<Rigidbody2D>();
playerSpawnPos = transform.position;
deathController = GetComponent<PlayerDeathController>();
spriteRendered = anim.gameObject.GetComponent<SpriteRenderer>();
if (spriteRendered.gameObject.transform.GetChild(0))
{
CableTarget = spriteRendered.gameObject.transform.GetChild(0);
}
CableNextDistance = CableMinDistance;
CableLineRendered.SetPosition(0, playerSpawnPos);
if (hasCable)
{
CableLineRendered.SetPosition(1, CableTarget.position);
}
CanEatLastCablePosition = false;
}
public float CabledSlowDownRate;
void Update()
{
UpdateEyesColor();
if (!levelStart.gamePlaying)
{
return;
}
UpdateCableLenght();
CableCurrentLenghtFromCable0 = CableLenghtUpToCable0();
if (GetBackAxisButtonDown() > 0 && HasCableMoreThan3MovablePoints() && hasCable)
{
DisableMovementTimer = 0.4f;
BashingAmount += Time.deltaTime * BashingPower;
BashingAmount = Mathf.Clamp(BashingAmount, 0, 1.5f);
}
else if (BashingAmount > 0)
{
BashingAmount -= Time.deltaTime;
if (BashingAmount < 0)
{
BashingAmount = 0;
}
}
// Go back on the cable
if (hasCable && (DisableMovementTimer > 0))
{
DisableMovementTimer -= Time.deltaTime;
//if (CableCurrentLenght >= CableMaxLength || BashingAmount >= BashingPowerMin)
{
if (HasCableMoreThan2MovablePoints())
{
int i = 2;
float Dist = Vector2.Distance(CableTarget.position, CableLineRendered.GetPosition(CableLineRendered.positionCount - i));
if (Dist <= CablePickupDistance)
{
i = 3;
}
Vector2 newVelocity = new Vector2(CableLineRendered.GetPosition(CableLineRendered.positionCount - i).x - CableTarget.position.x, CableLineRendered.GetPosition(CableLineRendered.positionCount - i).y - CableTarget.position.y);
// Method 1
//rb.AddForce(playerReverseSpeed * newVelocity);
//rb.velocity = Vector2.ClampMagnitude(rb.velocity, playerReverseSpeed);
CabledSlowDownRate = 1f;
if (CableCurrentLenghtFromCable0 < CablePullingSlowDownStartFromLast0)
{
CabledSlowDownRate = CableCurrentLenghtFromCable0 / CablePullingSlowDownStartFromLast0;
}
CabledSlowDownRate = Mathf.Pow(CabledSlowDownRate, 1.67f);
// Method 2
rb.velocity = playerReverseSpeed * (1f + BashingAmount) * newVelocity.normalized * CabledSlowDownRate;
// Correction: Useless
//Vector2 newPosition = rb.velocity * Time.deltaTime;
//Vector2 backVelocity = new Vector2(CableLineRendered.GetPosition(CableLineRendered.positionCount - i).x - newPosition.x, CableLineRendered.GetPosition(CableLineRendered.positionCount - i).y - newPosition.y);
//if (Vector2.Dot(newVelocity, backVelocity) < 0)
//{
// rb.velocity
//}
}
else
{
rb.velocity *= (1f - (Time.deltaTime * 3.73f));
}
}
ThrowPower = 0;
}
else //if (DisableMovementTimer <= 0)
{
if (GameIsThrowning)
{
float StickThrowPrecision = 0.994f;
Vector2 Stick = DetectPlayerInput();
if (ThrowPower > 0 && Stick.magnitude <= StickThrowPrecision)
{
ChargingThrow = false;
ThrowPower = ThrowPower / MaxThrowPower;
ThrowPower = Mathf.Pow(ThrowPower, 0.3f);
Vector2 VelocityDir = ThrowDirection.normalized;
//rb.velocity = playerSpeed * VelocityDir * ((VelocityMagnitude - DeadZone) / (1f - DeadZone));
rb.AddForce(playerSpeed * ThrowMultiplier * ThrowPower * VelocityDir);
//rb.velocity = Vector2.ClampMagnitude(rb.velocity, playerSpeed * 3);
ThrowPower = 0;
}
else if (Stick.magnitude > StickThrowPrecision)
{
ThrowDirection = -Stick;
ThrowPower += Time.deltaTime;
ThrowPower = Mathf.Clamp(ThrowPower, 0f, MaxThrowPower);
ChargingThrow = true;
}
else
{
ThrowPower = 0;
}
}
else
{
UpdateMovement(DetectPlayerInput());
}
}
if (HasCableMoreThan2MovablePoints() && hasCable)
{
Vector2 lastPointDirection2 = CableLineRendered.GetPosition(CableLineRendered.positionCount - 2) - CableTarget.position;
CanEatLastCablePosition = Vector2.Dot(rb.velocity.normalized, lastPointDirection2.normalized) > 0;
//if (GameIsThrowning)
//{
// CanEatLastCablePosition = false;
//}
// Collecting last cable position
if (CanEatLastCablePosition || DisableMovementTimer > 0 || CableCurrentLenght >= CableMaxLength)
{
float Dist2 = Vector2.Distance(CableTarget.position, CableLineRendered.GetPosition(CableLineRendered.positionCount - 2));
while (Dist2 <= CablePickupDistance && HasCableMoreThan2MovablePoints())
{
//CableCurrentLenght -= Vector2.Distance(CableLineRendered.GetPosition(CableLineRendered.positionCount - 2), CableLineRendered.GetPosition(CableLineRendered.positionCount - 3));
CableLineRendered.positionCount--;
CableLineRendered.SetPosition(CableLineRendered.positionCount - 1, CableTarget.position);
Dist2 = Vector2.Distance(CableTarget.position, CableLineRendered.GetPosition(CableLineRendered.positionCount - 2));
// Update CanEatLastCablePosition
lastPointDirection2 = CableLineRendered.GetPosition(CableLineRendered.positionCount - 2) - CableTarget.position;
CanEatLastCablePosition = Vector2.Dot(rb.velocity.normalized, lastPointDirection2.normalized) > 0;
}
}
}
else
{
CanEatLastCablePosition = false;
}
// Laying cable down
//if (BashingAmount < BashingPowerMin || !HasCableMoreThan2MovablePoints())
if (DisableMovementTimer <= 0 && (!CanEatLastCablePosition || !HasCableMoreThan2MovablePoints()) && hasCable)
{
float Dist = Vector2.Distance(CableTarget.position, CableLineRendered.GetPosition(CableLineRendered.positionCount - 2));
if (Dist >= CableNextDistance)
{
CableNextDistance = CableMinDistance; // - (Dist - CableNextDistance);
//CanEatLastCablePosition = false;
CableLineRendered.positionCount++;
CableLineRendered.SetPosition(CableLineRendered.positionCount - 2, CableTarget.position);
CableLineRendered.SetPosition(CableLineRendered.positionCount - 1, CableTarget.position);
}
}
// Update vibration
if (hasCable && (CableMaxLength - CableCurrentLenght <= CableVibrationStartFromEnd))
{
LastVibrationAlpha = (CableCurrentLenght - (CableMaxLength - CableVibrationStartFromEnd)) / CableVibrationStartFromEnd;
XInputDotNetPure.GamePad.SetVibration((PlayerIndex)GetVibrationPlayerNumber(), LastVibrationAlpha * VibrationIntensity, LastVibrationAlpha * VibrationIntensity);
}
else
{
XInputDotNetPure.GamePad.SetVibration((PlayerIndex)GetVibrationPlayerNumber(), 0, 0);
}
DetectPlayerAnimationDirection();
if (!hasCable && Vector2.Distance(CablePickupPosition, CableTarget.position) < CablePickupDistance)
{
hasCable = true;
if (CablePickupInstance)
{
Destroy(CablePickupInstance);
}
}
// Update cable position (last) in player's hand
if (hasCable)
{
CableLineRendered.SetPosition(CableLineRendered.positionCount - 1, CableTarget.position);
}
}
public float SlowDownRate;
public void UpdateMovement(Vector2 newVelocity)
{
//newVelocity = Vector2.ClampMagnitude(newVelocity.x, newVelocity.y), 1);
float VelocityMagnitude = newVelocity.magnitude;
SlowDownRate = (CableCurrentLenght - (CableMaxLength - CableSlowDownStartFromEnd)) / CableSlowDownStartFromEnd;
if (!hasCable || CableMaxLength - CableCurrentLenght > CableSlowDownStartFromEnd)
{
SlowDownRate = 0f;
}
SlowDownRate = Mathf.Pow(1 - SlowDownRate, 4);
if (VelocityMagnitude < DeadZone)
{
rb.velocity *= (1f - (Time.deltaTime * 0.5f)) * SlowDownRate;
//rb.velocity = Vector2.zero;
}
else if (!hasCable || CableCurrentLenght <= CableMaxLength - CableSlowDownStartFromEnd) // || BashingAmount >= BashingPowerMin)
{
//rb.inertia = 55;
Vector2 VelocityDir = newVelocity.normalized;
//rb.velocity = playerSpeed * VelocityDir * ((VelocityMagnitude - DeadZone) / (1f - DeadZone));
rb.AddForce((playerSpeed / 4) * VelocityDir * SlowDownRate * ((VelocityMagnitude - DeadZone) / (1f - DeadZone)));
rb.velocity = Vector2.ClampMagnitude(rb.velocity, playerSpeed);
}
else //if (hasCable && CableCurrentLenght > CableMaxLength - CableSlowDownStartFromEnd)
{
rb.velocity *= (1f - (Time.deltaTime * 0.5f)) * SlowDownRate;
}
}
void UpdateCableLenght()
{
CableCurrentLenght = 0f;
//for (int i = Cable0; i < CableLineRendered.positionCount - 1; ++i)
for (int i = 0; i < CableLineRendered.positionCount - 1; ++i)
{
CableCurrentLenght += Vector2.Distance(CableLineRendered.GetPosition(i), CableLineRendered.GetPosition(i + 1));
}
}
float CableLenghtUpToCable0()
{
float Lenght = 0f;
//for (int i = 0; i < Cable0 + 1; ++i)
//{
// Lenght += Vector2.Distance(CableLineRendered.GetPosition(i), CableLineRendered.GetPosition(i + 1));
//}
for (int i = Cable0; i < CableLineRendered.positionCount - 1; ++i)
{
Lenght += Vector2.Distance(CableLineRendered.GetPosition(i), CableLineRendered.GetPosition(i + 1));
}
return Lenght;
}
bool HasCableMoreThan2MovablePoints()
{
return CableLineRendered.positionCount > Cable0 + 2;
}
bool HasCableMoreThan3MovablePoints()
{
return CableLineRendered.positionCount > Cable0 + 3;
}
//
// Events
//
public void Finished()
{
isActive = false;
playerReverseSpeed = 0f;
playerSpeed = 0f;
rb.velocity = Vector2.zero;
rb.angularVelocity = 0f;
CableMaxLength = 99999f; //Fast hack to stop vibration
XInputDotNetPure.GamePad.SetVibration((PlayerIndex)GetVibrationPlayerNumber(), 0, 0);
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Enemy")
{
PlayerDies();
}
}
// Attach cable to Cabinet (Called by cabinet)
public void AttachCable(Transform cabinet, float NewCableLenght, bool IsStartingCabinet)
{
if (CableTarget)
{
CableLineRendered.SetPosition(CableLineRendered.positionCount - 1, CableTarget.position);
}
else
{
CableLineRendered.SetPosition(CableLineRendered.positionCount - 1, transform.position);
}
if (IsStartingCabinet)
{
hasCable = true;
CableLineRendered.SetPosition(CableLineRendered.positionCount - 2, cabinet.position);
CableMaxLength = NewCableLenght;
return;
}
UpdateCableLenght();
CableMaxLength = CableCurrentLenght + NewCableLenght;
Cable0 = CableLineRendered.positionCount - 1;
CableLineRendered.positionCount++;
if (CableTarget)
{
CableLineRendered.SetPosition(CableLineRendered.positionCount - 1, CableTarget.position);
}
else
{
CableLineRendered.SetPosition(CableLineRendered.positionCount - 1, transform.position);
}
CableLineRendered.SetPosition(CableLineRendered.positionCount - 2, cabinet.position);
}
public void PlayerDies()
{
if (hasCable)
{
DisableMovementTimer = 0f;
CanEatLastCablePosition = false;
hasCable = false;
CablePickupPosition = CableTarget.position;
if (CablePickupInstance)
{
Destroy(CablePickupInstance);
}
CablePickupInstance = GameManager.Instantiate(CablePickup);
CablePickupInstance.transform.position = CablePickupPosition;
}
//CableLineRendered.positionCount = 2;
deathController.ShowPlayerDies();
rb.velocity = Vector2.zero;
rb.angularVelocity = 0;
transform.position = playerSpawnPos;
}
private void OnDestroy()
{
XInputDotNetPure.GamePad.SetVibration((PlayerIndex)GetVibrationPlayerNumber(), 0, 0);
}
//
// Input
//
public void SetPlayerToGamepad(int gamepad)
{
gamepadAssigned = (Gamepads)gamepad;
if (gamepad == 0)
{
SetAnimDirection(false, false, false, true);
}
}
float GetBackAxis()
{
int PlayerN = (int)gamepadAssigned + 1;
string BackAxis = "Player" + PlayerN + "Back";
if (Input.GetAxisRaw(BackAxis + "Axis") > 0f)
{
return Input.GetAxisRaw(BackAxis + "Axis");
}
return Input.GetButton(BackAxis) == true ? 1 : 0;
}
float GetBackAxisButtonDown()
{
int PlayerN = (int)gamepadAssigned + 1;
string BackAxis = "Player" + PlayerN + "Back";
return Input.GetButtonDown(BackAxis) == true ? 1 : 0;
}
float GetXMovement()
{
int PlayerN = (int)gamepadAssigned + 1;
string Axis = "Player" + PlayerN + "Horizontal";
return Input.GetAxisRaw(Axis);
}
float GetYMovement()
{
int PlayerN = (int)gamepadAssigned + 1;
string Axis = "Player" + PlayerN + "Vertical";
return Input.GetAxisRaw(Axis);
}
int GetVibrationPlayerNumber()
{
//Vector2 asd1 = new Vector2(Input.GetAxisRaw("Player1Horizontal"), Input.GetAxisRaw("Player1Vertical"));
//Vector2 asd2 = new Vector2(XInputDotNetPure.GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.X, XInputDotNetPure.GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.Y);
//Vector2 asd21 = new Vector2(Input.GetAxisRaw("Player2Horizontal"), Input.GetAxisRaw("Player2Vertical"));
//Vector2 asd22 = new Vector2(XInputDotNetPure.GamePad.GetState(PlayerIndex.Two).ThumbSticks.Left.X, XInputDotNetPure.GamePad.GetState(PlayerIndex.Two).ThumbSticks.Left.Y);
//if (Vector2.Distance(asd1, asd2) <= Vector2.Distance(asd1, asd22))
//{
// return 1;
//}
//return 0;
//return (int)gamepadAssigned;
if ((int)gamepadAssigned == 0)
{
return 1;
}
else if ((int)gamepadAssigned == 1)
{
return 0;
}
else return (int)gamepadAssigned;
}
PlayerIndex GetPlayerIndex()
{
if ((int)gamepadAssigned == 0)
{
return PlayerIndex.One;
}
else if ((int)gamepadAssigned == 1)
{
return PlayerIndex.Two;
}
else if ((int)gamepadAssigned == 2)
{
return PlayerIndex.Three;
}
//else if ((int)gamepadAssigned == 3)
//{
//}
return PlayerIndex.Four;
}
Vector2 DetectPlayerInput()
{
float x = 0;
float y = 0;
switch (gamepadAssigned)
{
case Gamepads.GAMEPAD_1:
if (Input.GetAxisRaw("Player1Horizontal") > 0) { x = Input.GetAxisRaw("Player1Horizontal"); }
if (Input.GetAxisRaw("Player1Horizontal") < 0) { x = Input.GetAxisRaw("Player1Horizontal"); }
if (Input.GetAxisRaw("Player1Vertical") > 0) { y = Input.GetAxisRaw("Player1Vertical"); }
if (Input.GetAxisRaw("Player1Vertical") < 0) { y = Input.GetAxisRaw("Player1Vertical"); }
break;
case Gamepads.GAMEPAD_2:
if (Input.GetAxisRaw("Player2Horizontal") > 0) { x = Input.GetAxisRaw("Player2Horizontal"); }
if (Input.GetAxisRaw("Player2Horizontal") < 0) { x = Input.GetAxisRaw("Player2Horizontal"); }
if (Input.GetAxisRaw("Player2Vertical") > 0) { y = Input.GetAxisRaw("Player2Vertical"); }
if (Input.GetAxisRaw("Player2Vertical") < 0) { y = Input.GetAxisRaw("Player2Vertical"); }
break;
case Gamepads.GAMEPAD_3:
if (Input.GetAxisRaw("Player3Horizontal") > 0) { x = Input.GetAxisRaw("Player3Horizontal"); }
if (Input.GetAxisRaw("Player3Horizontal") < 0) { x = Input.GetAxisRaw("Player3Horizontal"); }
if (Input.GetAxisRaw("Player3Vertical") > 0) { y = Input.GetAxisRaw("Player3Vertical"); }
if (Input.GetAxisRaw("Player3Vertical") < 0) { y = Input.GetAxisRaw("Player3Vertical"); }
break;
case Gamepads.GAMEPAD_4:
if (Input.GetAxisRaw("Player4Horizontal") > 0) { x = Input.GetAxisRaw("Player4Horizontal"); }
if (Input.GetAxisRaw("Player4Horizontal") < 0) { x = Input.GetAxisRaw("Player4Horizontal"); }
if (Input.GetAxisRaw("Player4Vertical") > 0) { y = Input.GetAxisRaw("Player4Vertical"); }
if (Input.GetAxisRaw("Player4Vertical") < 0) { y = Input.GetAxisRaw("Player4Vertical"); }
break;
}
return new Vector2(x, y);
}
//
// Animations
//
void UpdateEyesColor()
{
// Toggle eyes color
ClosedEyesTimer += Time.deltaTime;
while (eyesOpened ? ClosedEyesTimer >= OpenedEyesTime : ClosedEyesTimer >= ClosedEyesTime)
{
if (eyesOpened)
{
ClosedEyesTimer -= OpenedEyesTime;
}
else
{
ClosedEyesTimer -= ClosedEyesTime;
}
eyesOpened = !eyesOpened;
if (!eyesOpened)
{
spriteRendered.color = Color.black;
}
else
{
spriteRendered.color = Color.white;
}
}
}
void DetectPlayerAnimationDirection()
{
float x = 0;
float y = 0;
if (rb.velocity.magnitude < 0.01f)
{
SetAnimDirection(false, false, false, true);
anim.Play(anim.GetCurrentAnimatorStateInfo(0).fullPathHash);
// Keep same anim Direction
}
else if (DisableMovementTimer > 0 || GameIsThrowning)
{
if (Mathf.Abs(rb.velocity.x) >= Mathf.Abs(rb.velocity.y))
{
if (rb.velocity.x >= 0)
{
SetAnimDirection(false, false, false, true);
}
else
{
SetAnimDirection(false, false, true, false);
}
}
else //if (Mathf.Abs(rb.velocity.x) < Mathf.Abs(rb.velocity.y))
{
if (rb.velocity.y >= 0)
{
SetAnimDirection(true, false, false, false);
}
else
{
SetAnimDirection(false, true, false, false);
}
}
}
else if (Mathf.Abs(GetXMovement()) < DeadZone && Mathf.Abs(GetYMovement()) < DeadZone)
{
SetAnimDirection(false, false, false, true);
// Keep same anim Direction
}
else if (Mathf.Abs(GetXMovement()) > Mathf.Abs(GetYMovement()))
{
if (GetXMovement() >= 0)
{
SetAnimDirection(false, false, false, true);
}
else
{
SetAnimDirection(false, false, true, false);
}
}
else //if (Mathf.Abs(GetXMovement()) <= Mathf.Abs(GetYMovement()))
{
if (GetYMovement() >= 0)
{
SetAnimDirection(true, false, false, false);
}
else
{
SetAnimDirection(false, true, false, false);
}
}
anim.SetBool("PullingCable", DisableMovementTimer > 0);
anim.SetFloat("WalkingSpeed", rb.velocity.magnitude * 0.89f);
anim.SetBool("HasCable", hasCable);
}
void SetAnimDirection(bool up, bool down, bool left, bool right)
{
if (isSpaceShip)
{
if (rb)
{
float angle = Vector2.Angle(Vector2.up, new Vector2(rb.velocity.x, rb.velocity.y));
Vector2 normVel = rb.velocity.normalized;
normVel = ThrowDirection.normalized;
angle = Mathf.Atan2(normVel.x, normVel.y) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(-angle, Vector3.forward);
rb.rotation = -angle;
rb.angularVelocity = 0;
}
}
else
{
anim.SetBool("WalkingUp", up);
anim.SetBool("WalkingDown", down);
anim.SetBool("WalkingLeft", left);
anim.SetBool("WalkingRight", right);
}
}
}