blob: add03ba4cf2f78a159bdac713b6cf070b5f3cbfb (
plain) (
blame)
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
|
using UnityEngine;
[RequireComponent(typeof(PlayerControl))]
[RequireComponent(typeof(Rigidbody))]
public class PlayerMovement : MonoBehaviour
{
private static readonly float BoostAccel = 10;
public float movementSpeed = 5;
public float movementSpeedOnGrabbing = 3;
public float boostFalloffLerp = 0.025f;
private PlayerControl _playerControl;
private Rigidbody _rigidbody;
private float _currentBoost;
private bool CanBoost => _currentBoost <= 0.1f;
private void Awake()
{
_playerControl = GetComponent<PlayerControl>();
_rigidbody = GetComponent<Rigidbody>();
}
private void Update()
{
if (_playerControl.boosting && CanBoost)
{
_currentBoost = BoostAccel;
}
}
private void FixedUpdate()
{
FixedUpdateMovement();
FixedUpdateBoostFalloff();
}
private void FixedUpdateMovement()
{
var velocity = _rigidbody.linearVelocity;
var speed = _playerControl.grabbing ? movementSpeedOnGrabbing : movementSpeed + _currentBoost;
var direction = new Vector3(
_playerControl.movementHorizontal,
0,
_playerControl.movementVertical)
.normalized;
var playerVelocity = speed * direction + velocity.y * Vector3.up;
_rigidbody.linearVelocity = playerVelocity;
}
private void FixedUpdateBoostFalloff()
{
_currentBoost = Mathf.Lerp(_currentBoost, 0, boostFalloffLerp);
}
}
|