/
PlayerMovement.cs
165 lines (141 loc) · 4.4 KB
/
PlayerMovement.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Rewired;
[RequireComponent(typeof(AnimCont))]
[RequireComponent(typeof(Rigidbody))]
public class PlayerMovement : MonoBehaviour
{
public CrackInstance crackInst;
public bool punching = false;
[SerializeField] bool canMove = false;
[SerializeField] float moveSpeed = 5f;
[SerializeField] int playerId = 0;
[SerializeField] GameObject pillBody;
[SerializeField] GameObject cameraAngle;
[SerializeField] GameObject stunStars;
private AnimCont animCont;
private Rigidbody _rb;
private Vector3 moveVect;
private Vector3 movementDir;
private bool walking = false;
private bool repairing = false;
// Rewired stuff
private bool initialized;
private Player player; // The Rewired Player
private void Start()
{
animCont = GetComponent<AnimCont>();
_rb = GetComponent<Rigidbody>();
StartCoroutine(Initialize());
}
/// <summary>
/// Get the Rewired Player object for this player.
/// </summary>
private IEnumerator Initialize()
{
player = ReInput.players.GetPlayer(playerId);
canMove = false;
yield return new WaitForSeconds(1f);
canMove = true;
initialized = true;
}
private void Update()
{
if (!ReInput.isReady) return; // Exit if Rewired isn't ready. This would only happen during a script recompile in the editor.
if (!initialized) Initialize(); // Reinitialize after a recompile in the editor
GetInput();
}
private void FixedUpdate()
{
if (!ReInput.isReady) return; // Exit if Rewired isn't ready. This would only happen during a script recompile in the editor.
if (!initialized) Initialize(); // Reinitialize after a recompile in the editor
if(canMove)
ProcessInput();
}
private void GetInput()
{
// Movement
moveVect = (cameraAngle.transform.right * player.GetAxis("MoveHorizontal")) +
(cameraAngle.transform.forward * player.GetAxis("MoveVertical"));
moveVect.y = 0;
moveVect = moveVect.normalized * moveSpeed;
// Repair
if(player.GetButtonDown("Repair") && crackInst && !crackInst.beingFixed && playerId == 1)
{
// Start repairing
repairing = true;
animCont.SetRepair();
canMove = false;
_rb.velocity = Vector3.zero;
Vector3 crackPos = crackInst.transform.position;
Vector3 posLookAt = new Vector3(crackPos.x, pillBody.transform.position.y, crackPos.z);
pillBody.transform.LookAt(posLookAt);
crackInst.beingFixed = true;
}
/*else if(player.GetButtonUp("Repair"))
{
// Stop repairing
repairing = false;
animCont.SetRepair(false);
}*/
// Punch
if (player.GetButtonDown("Repair") && playerId == 0 && !punching)
{
// Animation
punching = true;
AudioManager.instance.PlaySwingPunch();
animCont.DoPunch();
}
}
private void ProcessInput()
{
// Movement
if(moveVect.magnitude > 0.0f)
{
_rb.velocity = moveVect;
Vector3 lookVect = new Vector3(moveVect.x, 0, moveVect.z) + transform.position;
pillBody.transform.LookAt(lookVect);
if(!walking)
{
animCont.SetWalking(true);
walking = true;
}
}
else
{
_rb.velocity = Vector3.zero;
if (walking)
{
animCont.SetWalking(false);
walking = false;
}
}
}
public void EndRepairs()
{
if (crackInst)
{
StartCoroutine(crackInst.FixCrack());
AudioManager.instance.PlaySlapBandaid();
crackInst = null;
}
canMove = true;
}
private void OnTriggerEnter(Collider other)
{
if(other.tag == "Enemy" && playerId == 1)
{
canMove = false;
_rb.velocity = Vector3.zero;
stunStars.SetActive(true);
AudioManager.instance.PlayDizzy();
StartCoroutine(animCont.BeStun());
}
}
public void StunOver()
{
canMove = true;
stunStars.SetActive(false);
}
}