added speed change and death
This commit is contained in:
1 parent
9e5f46b48c
commit
08e050ffd3
6 files changed
+1437
-5
No files matched your search
@@ -5,14 +5,18 @@ namespace Code.Gameplay.Player
|
||||
public class PlayerController : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private float forwardSpeed = 10f;
|
||||
[SerializeField] private float maxSpeed = 40f;
|
||||
[SerializeField] private float laneDistance = 5f;
|
||||
[SerializeField] private float jumpForce = 8f;
|
||||
[SerializeField] private float jumpForce = 11f;
|
||||
[SerializeField] private float gravity = 20f;
|
||||
|
||||
private int _targetLane = 1;
|
||||
|
||||
private CharacterController _controller;
|
||||
private Vector3 _direction;
|
||||
|
||||
public System.Action OnPlayerDeath;
|
||||
private bool _isDead = false;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
@@ -21,6 +25,8 @@ namespace Code.Gameplay.Player
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (_isDead) return;
|
||||
|
||||
if (UnityEngine.Input.GetKeyDown(KeyCode.RightArrow) || UnityEngine.Input.GetKeyDown(KeyCode.D))
|
||||
{
|
||||
_targetLane = Mathf.Min(_targetLane + 1, 2);
|
||||
@@ -66,11 +72,16 @@ namespace Code.Gameplay.Player
|
||||
var finalMove = new Vector3(moveX, _direction.y * Time.deltaTime, _direction.z * Time.deltaTime);
|
||||
|
||||
_controller.Move(finalMove);
|
||||
CheckForDeath();
|
||||
}
|
||||
|
||||
public void IncreaseSpeed(float amount)
|
||||
{
|
||||
forwardSpeed += amount;
|
||||
if (forwardSpeed < maxSpeed)
|
||||
{
|
||||
var desirableSpeed = forwardSpeed + amount;
|
||||
forwardSpeed = Mathf.Min(desirableSpeed, maxSpeed);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnTriggerEnter(Collider foreign)
|
||||
@@ -82,5 +93,23 @@ namespace Code.Gameplay.Player
|
||||
Destroy(foreign.gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
private void CheckForDeath()
|
||||
{
|
||||
if (_isDead) return;
|
||||
|
||||
if (Time.timeSinceLevelLoad > 1f && _controller.velocity.z <= 0.1f)
|
||||
{
|
||||
Die();
|
||||
}
|
||||
}
|
||||
|
||||
private void Die()
|
||||
{
|
||||
_isDead = true;
|
||||
forwardSpeed = 0;
|
||||
_direction = Vector3.zero;
|
||||
OnPlayerDeath?.Invoke();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user