-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlayerMovement.cs
132 lines (121 loc) · 4.29 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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public CharacterController controller;
public GameObject character;
private AudioSource footsteps;
private bool isWalking = false;
public float speed = 4f;
private float currentSpeed;
public float gravity = -10f;
public float jumpForce = 1.5f;
public bool isGrounded;
public Transform groundCheck;
public float groundDistance;
public LayerMask groundMask;
private Vector3 velocity;
private PlayerController player;
public Animator animator;
private bool prevState;
private bool firstPressed = false;
private bool firstReleased = false;
private float startY;
// Start is called before the first frame update
void Start()
{
currentSpeed = speed;
player = gameObject.GetComponent<PlayerController>();
prevState = player.canShoot;
startY = character.transform.localPosition.y;
footsteps = GetComponent<AudioSource>();
footsteps.pitch = 1.25f;
}
// Update is called once per frame
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if (isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
if ((x != 0 || z != 0) && !isWalking)
{
isWalking = true;
footsteps.Play(0);
}
else
{
isWalking = false;
footsteps.Pause();
}
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(currentSpeed * Time.deltaTime * move);
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
if (Input.GetButtonDown("Jump") && isGrounded)
{
velocity.y = Mathf.Sqrt(jumpForce * -1 * gravity);
}
if (Input.GetKey(KeyCode.LeftShift) && isGrounded && isWalking)
{
animator.SetBool("isRunning", true);
footsteps.pitch = 2f;
currentSpeed = speed * 2;
if (!firstPressed)
{
prevState = player.canShoot;
firstPressed = true;
firstReleased = false;
}
player.canShoot = false;
}
else
{
footsteps.pitch = 1.25f;
animator.SetBool("isRunning", false);
currentSpeed = speed;
if (!firstReleased)
{
firstReleased = true;
firstPressed = false;
player.canShoot = prevState;
}
}
if (Input.GetKeyDown(KeyCode.LeftControl) && isGrounded)
{
character.transform.localPosition = new Vector3(character.transform.localPosition.x, -0.3f, character.transform.localPosition.z);
currentSpeed = speed / 2;
controller.height = 1f;
player.GetComponent<CapsuleCollider>().height = 1;
}
if (Input.GetKeyUp(KeyCode.LeftControl))
{
character.transform.localPosition = new Vector3(character.transform.localPosition.x, startY, character.transform.localPosition.z);
currentSpeed = speed;
controller.height = 2;
player.GetComponent<CapsuleCollider>().height = 2;
}
/*
if (Input.GetKeyDown(KeyCode.Q))
{
transform.localRotation = Quaternion.Euler(transform.localRotation.x, transform.localRotation.y, transform.localRotation.z + 30f);
}
if (Input.GetKeyUp(KeyCode.Q))
{
transform.localRotation = Quaternion.Euler(transform.localRotation.x, transform.localRotation.y, transform.localRotation.z - 30f);
}
if (Input.GetKeyDown(KeyCode.E))
{
transform.RotateAround(groundCheck.position, Vector3.right, 30f);
}
if (Input.GetKeyUp(KeyCode.E))
{
transform.RotateAround(groundCheck.position, Vector3.right, -30f);
}
*/
}
}