-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayerMovement.cs
64 lines (51 loc) · 1.8 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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
[SerializeField] private string horizontalInputName;
[SerializeField] private string verticalInputName;
[SerializeField] private float movementSpeed;
[SerializeField] private AnimationCurve jumpFallOff;
[SerializeField] private float jumpMultiplier;
[SerializeField] private KeyCode jumpKey;
private CharacterController charController;
private bool isJumping;
private void Awake()
{
charController = GetComponent<CharacterController>();
}
private void Update()
{
PlayerMove();
}
private void PlayerMove()
{
float horizInput = Input.GetAxis("Horizontal") * movementSpeed;
float vertInput = Input.GetAxis("Vertical") * movementSpeed;
Vector3 forwardMovement = transform.forward * vertInput;
Vector3 rightMovement = transform.right * horizInput;
charController.SimpleMove(forwardMovement + rightMovement);
JumpInput();
}
private void JumpInput()
{
if(Input.GetKeyDown(jumpKey) && !isJumping)
{
isJumping = true;
StartCoroutine(JumpEvent());
}
}
private IEnumerator JumpEvent()
{
charController.slopeLimit = 45.0f;
float timeInAir = 0.0f;
do
{
float jumpForce = jumpFallOff.Evaluate(timeInAir);
charController.Move(Vector3.up * jumpForce * jumpMultiplier * Time.deltaTime);
timeInAir += Time.deltaTime;
yield return null;
} while (!charController.isGrounded && charController.collisionFlags != CollisionFlags.Above);
isJumping = false;
}
}