-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerMovement with Unity Proton
More file actions
119 lines (100 loc) · 2.48 KB
/
PlayerMovement with Unity Proton
File metadata and controls
119 lines (100 loc) · 2.48 KB
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
using Photon.Pun;
using UnityEngine;
public class PlayerMovement : MonoBehaviourPunCallbacks
{
[Range(0.1f, 1000f)]
public float moveSpeed = 1f;
[Range(0f, 1000f)]
public int runningBackwards = 50;
[Range(1f, 250f)]
public float jumpStrength = 10f;
[Range(0f, 100f)]
public int inAirPaneltyPercentage = 90;
[Range(0.1f, 100f)]
public float gravity = 2f;
[Range(0.1f, 100f)]
public float maxGravity = 100f;
private CharacterController characterController;
private bool isGoingBackwars = false;
private bool isGrounded = true;
private bool isRunning;
private float verticalVelocity = 1f;
private Vector3 move = Vector3.zero;
public PhotonView view;
private void Awake()
{
characterController = GetComponent<CharacterController>();
CameraWork _cameraWork = base.gameObject.GetComponent<CameraWork>();
if (_cameraWork != null)
{
if (base.photonView.IsMine)
{
_cameraWork.OnStartFollowing();
}
}
else
{
Debug.LogError("<Color=Red><a>Missing</a></Color> CameraWork Component on playerPrefab.", this);
}
}
private void Update()
{
if (view.IsMine)
{
CheckPlayerGoingBackwards();
isGrounded = characterController.isGrounded;
MovePlayer();
}
}
private void MovePlayer()
{
float inputHorizontal = Input.GetAxis("Horizontal");
float inputVertical = Input.GetAxis("Vertical");
Vector3 lateralMove = (base.transform.forward * inputVertical + base.transform.right * inputHorizontal) * (moveSpeed / 8f);
if (!isGrounded)
{
lateralMove *= 1f - (float)inAirPaneltyPercentage / 100f;
}
else if (isGoingBackwars)
{
lateralMove *= 1f - (float)runningBackwards / 100f;
}
move.x = lateralMove.x;
move.z = lateralMove.z;
ApplyGravity();
ApplyJump();
characterController.Move(move * Time.deltaTime);
if (Input.GetKeyDown(KeyCode.LeftShift))
{
moveSpeed = 750f;
isRunning = true;
}
if (Input.GetKeyUp(KeyCode.LeftShift))
{
moveSpeed = 200f;
isRunning = false;
}
}
private void ApplyGravity()
{
if (isGrounded)
{
verticalVelocity = 0f;
}
verticalVelocity -= Mathf.Sqrt(gravity * Time.fixedDeltaTime);
verticalVelocity = Mathf.Clamp(verticalVelocity, 0f - maxGravity, float.MaxValue);
move.y = verticalVelocity;
}
private void ApplyJump()
{
if (Input.GetButton("Jump") && isGrounded)
{
verticalVelocity += Mathf.Sqrt(jumpStrength * 2f * gravity);
move.y = verticalVelocity;
}
}
private void CheckPlayerGoingBackwards()
{
isGoingBackwars = Input.GetAxis("Vertical") <= -1f;
}
}