-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMoveController.cs
More file actions
67 lines (53 loc) · 1.97 KB
/
MoveController.cs
File metadata and controls
67 lines (53 loc) · 1.97 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
using UnityEngine;
namespace AK.CompetitiveController
{
public class MoveController
{
private readonly MoveConfig _config;
private readonly Capsule _capsule;
private Vector3 _velocity, _position;
private Vector2 _delta;
private Quaternion _rotation;
private bool _jump, _grounded;
public MoveController(MoveConfig config, CapsuleCollider capsuleCollider, Vector3 position)
{
_config = config;
_capsule = new Capsule(capsuleCollider, config);
_position = position;
}
public bool Grounded => _grounded;
public Vector3 Position => _position;
public Vector3 Velocity => _velocity;
public void Update(Vector2 delta, Quaternion rotation, bool jump)
{
_delta = delta;
_rotation = rotation;
_jump = jump;
(_, _, _grounded) = _capsule.Trace(Vector3.down * .1f, _config.groundLayers);
CalculateVelocity();
_position += _velocity * Time.deltaTime;
_capsule.ResolveCollisions(ref _position, ref _velocity, _config.groundLayers);
}
private void CalculateVelocity()
{
var forward = _rotation * Vector3.forward;
var right = _rotation * Vector3.right;
var direction = new Vector3(
forward.x * _delta.y + right.x * _delta.x,
0,
forward.z * _delta.y + right.z * _delta.x);
direction.Normalize();
_velocity.y -= _config.Gravity * Time.deltaTime;
if (!_grounded)
{
_velocity -= 1 / _config.AirFriction * Time.deltaTime * _velocity;
}
else
{
_velocity = _config.Speed * direction;
if (_jump)
_velocity.y += _config.JumpPower;
}
}
}
}