-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlayerScript.cs
173 lines (138 loc) · 5.01 KB
/
PlayerScript.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerScript : MonoBehaviour
{
[Header("Player Movement")]
public float playerSpeed = 1.90f;
public float playerSprint = 3f;
[Header("Player Health Things")]
private float playerHealth = 120f;
public float PresentHealth;
public HealthBar healthBar;
public AudioClip playerHeartSound;
public AudioSource audioSource;
public GameObject playerUI;
public GameObject miniMap;
public GameObject miniMapCam;
public GameObject playerProp;
[Header("Player Script Camera")]
public Transform playerCam;
public GameObject deathCamera;
public GameObject endGameMenu;
[Header("Player Animator & Gravity")]
public CharacterController characterCntrl;
public float gravity = -9.81f;
public Animator animator;
[Header("Player Jumping & Velocity")]
public float turnCalmTime = 0.1f;
float turnCalmVelocity;
public float jumpRange= 1f;
Vector3 veclocity;
public Transform surfaceCheck;
bool onSurface;
public float SurfaceDistance = 0.4f;
public LayerMask surfaceMask;
private void Start()
{
Cursor.lockState = CursorLockMode.Locked;
PresentHealth = playerHealth;
healthBar.GiveFullHealth(playerHealth);
}
private void Update()
{
onSurface = Physics.CheckSphere(surfaceCheck.position, SurfaceDistance, surfaceMask);
if(onSurface && veclocity.y < 0)
{
veclocity.y = -2f;
}
veclocity.y += gravity * Time.deltaTime;
characterCntrl.Move(veclocity * Time.deltaTime);
PlayerMove();
Jump();
PlayerSprint();
}
void PlayerMove()
{
float horizontal_axis = Input.GetAxisRaw("Horizontal");
float vertical_axis = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(horizontal_axis, 0f, vertical_axis).normalized;
if(direction.magnitude >= 0.1f)
{
animator.SetBool("Idle", false);
animator.SetBool("Walk", true);
animator.SetBool("Running", false);
animator.SetBool("RifleWalk", false);
animator.SetBool("IdleAim", false);
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + playerCam.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnCalmVelocity, turnCalmTime);
transform.rotation = Quaternion.Euler(0f,angle,0f);
Vector3 moveDirection = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
characterCntrl.Move(moveDirection.normalized * playerSpeed * Time.deltaTime);
}
else
{
animator.SetBool("Idle", true);
animator.SetBool("Walk", false);
animator.SetBool("Running", false);
}
}
void Jump()
{
if (Input.GetButtonDown("Jump") && onSurface)
{
animator.SetBool("Jump", true);
veclocity.y = Mathf.Sqrt(jumpRange * -2 * gravity);
}
else
{
animator.SetBool("Idle", true);
animator.SetBool("Jump", false);
}
}
void PlayerSprint()
{
if(Input.GetButton("Sprint") && Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow) && onSurface)
{
float horizontal_axis = Input.GetAxisRaw("Horizontal");
float vertical_axis = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(horizontal_axis, 0f, vertical_axis).normalized;
if (direction.magnitude >= 0.1f)
{
animator.SetBool("Walk", false);
animator.SetBool("Running", true);
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + playerCam.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnCalmVelocity, turnCalmTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
Vector3 moveDirection = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
characterCntrl.Move(moveDirection.normalized * playerSprint * Time.deltaTime);
}
else
{
animator.SetBool("Walk", true);
animator.SetBool("Running", false);
}
}
}
public void PlayerHitDamage(float takeDamage)
{
PresentHealth -= takeDamage;
healthBar.SetHealth(PresentHealth);
audioSource.PlayOneShot(playerHeartSound);
if (PresentHealth <= 0)
{
PlayerDie();
}
}
private void PlayerDie()
{
playerProp.SetActive(false);
miniMap.SetActive(false);
miniMapCam.SetActive(false);
playerUI.SetActive(false);
endGameMenu.SetActive(true);
Cursor.lockState = CursorLockMode.None;
deathCamera.SetActive(true);
Object.Destroy(gameObject);
}
}