using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
|
|
[RequireComponent(typeof(PlayerInput), typeof(CharacterController))]
|
|
public class PlayerInputController : MonoBehaviour
|
|
{
|
|
|
|
|
|
[SerializeField]
|
|
private float m_playerSpeed = 1;
|
|
|
|
[SerializeField]
|
|
private float m_rotationSpeed = 1;
|
|
|
|
[SerializeField]
|
|
[Tooltip("Used to spin on spot\n"
|
|
+ "0 = back\n"
|
|
+ "0.5 = left/right\n"
|
|
+ "1.0 = forward")]
|
|
private AnimationCurve m_turnRadius;
|
|
|
|
|
|
private PlayerInput m_input;
|
|
private CharacterController m_controller;
|
|
|
|
private Vector3 m_desiredDirection;
|
|
private bool m_recievedInput => m_desiredDirection.magnitude != 0;
|
|
|
|
private YeetController m_yeetController;
|
|
|
|
private void Awake()
|
|
{
|
|
m_input = GetComponent<PlayerInput>();
|
|
m_controller = GetComponent<CharacterController>();
|
|
m_yeetController = GetComponent<YeetController>();
|
|
}
|
|
|
|
|
|
private void Update()
|
|
{
|
|
ApplyRotation();
|
|
ApplyMovement();
|
|
LockAxis(Vector3.up);
|
|
}
|
|
|
|
|
|
private void OnMovement(InputValue value)
|
|
{
|
|
Vector2 m_recievedInput = value.Get<Vector2>();
|
|
m_desiredDirection = new Vector3(m_recievedInput.x, 0.0f, m_recievedInput.y);
|
|
}
|
|
|
|
|
|
private void ApplyRotation()
|
|
{
|
|
transform.forward = Vector3.Slerp(transform.forward, m_desiredDirection.normalized, m_rotationSpeed * Time.deltaTime);
|
|
}
|
|
|
|
private void ApplyMovement()
|
|
{
|
|
if (!m_recievedInput)
|
|
return;
|
|
|
|
|
|
float forwardRatio = (Vector3.Dot(transform.forward, m_desiredDirection.normalized) + 1) / 2;
|
|
float speed = m_turnRadius.Evaluate(forwardRatio) * m_playerSpeed;
|
|
|
|
|
|
m_controller.Move(transform.forward * speed * Time.deltaTime);
|
|
}
|
|
|
|
private void LockAxis(Vector3 axis)
|
|
{
|
|
transform.position = Vector3.ProjectOnPlane(transform.position, axis);
|
|
}
|
|
}
|