-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlayerBaseMovement
113 lines (87 loc) · 2.75 KB
/
PlayerBaseMovement
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(CharacterController))]
public abstract class PlayerBaseMovement : MonoBehaviour
{
protected CharacterController controller;
[SerializeField] protected float moveSpeed = 5;
[SerializeField] protected float jumpForce = 10;
[SerializeField] protected float velocityY = 0;
[SerializeField] protected float gravity = -9.81f;
//[SerializeField] protected float gravityMultiplier = 2;
[SerializeField] protected float mass = 1f;
[SerializeField] protected float damping = 5;
private protected Vector3 moveInputVector = Vector3.zero;
private protected Vector3 currentImpact = Vector3.zero;
private protected bool isJumpButtonPressed = false;
protected virtual void GetInput()
{
}
protected virtual void Movement()
{
}
protected virtual void Jump()
{
}
protected virtual void DefaultUpdate()
{
}
protected void AssignCharacterController()
{
controller = GetComponent<CharacterController>();
if (controller == null)
{
Debug.Log("Missing: Character Controller reference");
}
}
protected virtual void DefaultGetInput()
{
moveInputVector = new Vector3(Input.GetAxisRaw(Axis.HORIZONTAL), 0, Input.GetAxisRaw(Axis.VERTICAL));
isJumpButtonPressed = Input.GetKeyDown(KeyCode.Space);
}
protected virtual void DefaultMovement()
{
moveInputVector = transform.TransformDirection(moveInputVector);
if (controller.isGrounded && velocityY < 0f)
{
velocityY = 0f;
//ResetImpactY();
}
velocityY += gravity * Time.deltaTime;
/*if (velocityY < gravity * gravityMultiplier)
{
velocityY = gravity * gravityMultiplier;
}*/
Vector3 velocity = (moveInputVector * moveSpeed) + (Vector3.up * velocityY);
if (currentImpact.magnitude > 0.2f)
{
velocity += currentImpact;
}
controller.Move(velocity * Time.deltaTime);
currentImpact = Vector3.Lerp(currentImpact, Vector3.zero, damping * Time.deltaTime);
}
protected virtual void DefaultJump()
{
if (isJumpButtonPressed)
{
if (controller.isGrounded)
{
AddForce(Vector3.up, jumpForce);
//velocityY = jumpForce;
}
}
}
public void AddForce(Vector3 direction, float magnitude)
{
currentImpact += direction.normalized * magnitude / mass;
}
protected void ResetImpact()
{
currentImpact = Vector3.zero;
}
protected void ResetImpactY()
{
currentImpact.y = 0f;
}
}