59 lines
1.5 KiB
C#
59 lines
1.5 KiB
C#
|
|
using UnityEngine;
|
||
|
|
|
||
|
|
public class PlayerMovement : MonoBehaviour
|
||
|
|
{
|
||
|
|
public float moveSpeed = 5f;
|
||
|
|
public float rotationSpeed = 12f;
|
||
|
|
|
||
|
|
private CharacterController characterController;
|
||
|
|
private float speedMultiplier = 1f;
|
||
|
|
private float buffTimer;
|
||
|
|
|
||
|
|
private void Awake()
|
||
|
|
{
|
||
|
|
characterController = GetComponent<CharacterController>();
|
||
|
|
}
|
||
|
|
|
||
|
|
private void Update()
|
||
|
|
{
|
||
|
|
Vector3 input = new Vector3(Input.GetAxisRaw("Horizontal"), 0f, Input.GetAxisRaw("Vertical"));
|
||
|
|
if (input.sqrMagnitude > 1f)
|
||
|
|
{
|
||
|
|
input.Normalize();
|
||
|
|
}
|
||
|
|
|
||
|
|
Vector3 move = input * moveSpeed * speedMultiplier;
|
||
|
|
move.y = Physics.gravity.y * 0.2f;
|
||
|
|
|
||
|
|
if (characterController != null)
|
||
|
|
{
|
||
|
|
characterController.Move(move * Time.deltaTime);
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
transform.position += move * Time.deltaTime;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (input.sqrMagnitude > 0.01f)
|
||
|
|
{
|
||
|
|
Quaternion targetRotation = Quaternion.LookRotation(input, Vector3.up);
|
||
|
|
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (buffTimer > 0f)
|
||
|
|
{
|
||
|
|
buffTimer -= Time.deltaTime;
|
||
|
|
if (buffTimer <= 0f)
|
||
|
|
{
|
||
|
|
speedMultiplier = 1f;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public void ApplySpeedBuff(float multiplier, float duration)
|
||
|
|
{
|
||
|
|
speedMultiplier = Mathf.Max(1f, multiplier);
|
||
|
|
buffTimer = Mathf.Max(0f, duration);
|
||
|
|
}
|
||
|
|
}
|