using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Player
{
interface IPlayerPossibility
{
void RigidbodyMovements(); // Rigidbody physic movements
void TransformMovements(); // Transform movements
}
public class PlayerController : MonoBehaviour, IPlayerPossibility
{
private Rigidbody playerRb;
public float speed;
public float boostSpeed;
private float normalSpeed = 10.0f;
private float countdownSpeed = 1.5f;
public float jumpSpeed;
private bool jumpReady;
private float jumpCD = 1.2f;
private float jumpCDCurrent = 0.0f;
// Start is called before the first frame update
void Start() => playerRb = GetComponent<Rigidbody>();
void FixedUpdate() => RigidbodyMovements();
public void RigidbodyMovements()
{
float inputVertical = Input.GetAxis("Vertical");
float inputHorizontal = Input.GetAxis("Horizontal");
Vector3 playerMovement = new Vector3(inputHorizontal, 0, inputVertical);
playerRb.AddRelativeForce(playerMovement * speed);
Jump();
}
public void TransformMovements()
{
float inputVertical = Input.GetAxis("Vertical");
float inputHorizontal = Input.GetAxis("Horizontal");
Vector3 playerMovement = new Vector3(inputHorizontal, 0, inputVertical) * speed * Time.deltaTime;
transform.Translate(playerMovement, Space.Self);
Jump();
}
private void Jump()
{
if (jumpCDCurrent >= jumpCD)
{
jumpReady = true;
}
else
{
jumpReady = false;
jumpCDCurrent += Time.deltaTime;
jumpCDCurrent = Mathf.Clamp(jumpCDCurrent, 0.0f, jumpCD);
}
if (Input.GetKeyDown(KeyCode.Space) && jumpReady)
{
playerRb.AddForce(Vector3.up * jumpSpeed, ForceMode.Impulse);
jumpCDCurrent = 0.0f;
}
}
private void OnTriggerEnter(Collider other)
{
if(other.CompareTag("Coin"))
{
speed = boostSpeed;
StartCoroutine("BoostSpeed");
}
}
private IEnumerator BoostSpeed()
{
yield return new WaitForSeconds(countdownSpeed);
speed = normalSpeed;
}
}
}