using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
|
|
[RequireComponent(typeof(CharacterController))]
|
|
public class PlayerInputController : InputBehaviour
|
|
{
|
|
|
|
|
|
[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;
|
|
|
|
[SerializeField]
|
|
private Animator m_legAnimator;
|
|
|
|
private CharacterController m_controller;
|
|
|
|
private Vector3 m_desiredDirection;
|
|
private bool m_recievedInput => m_desiredDirection.magnitude != 0;
|
|
|
|
|
|
private void Awake()
|
|
{
|
|
m_controller = GetComponent<CharacterController>();
|
|
}
|
|
|
|
|
|
private void Update()
|
|
{
|
|
ApplyRotation();
|
|
ApplyMovement();
|
|
LockAxis(Vector3.up);
|
|
}
|
|
|
|
|
|
private void OnMovement(InputAction.CallbackContext value)
|
|
{
|
|
Vector2 m_recievedInput = value.ReadValue<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()
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
float forwardRatio = (Vector3.Dot(transform.forward, m_desiredDirection.normalized) + 1) / 2;
|
|
float speed = m_turnRadius.Evaluate(forwardRatio) * m_playerSpeed;
|
|
m_legAnimator.SetFloat("Movement", speed);
|
|
|
|
if (!m_recievedInput)
|
|
return;
|
|
|
|
m_controller.Move(transform.forward * speed * Time.deltaTime);
|
|
}
|
|
|
|
private void LockAxis(Vector3 axis)
|
|
{
|
|
transform.position = Vector3.ProjectOnPlane(transform.position, axis);
|
|
}
|
|
|
|
public override void RegisterInput(PlayerInput playerInput)
|
|
{
|
|
playerInput.currentActionMap.FindAction("Movement").performed += OnMovement;
|
|
playerInput.currentActionMap.FindAction("Movement").canceled += OnMovement;
|
|
}
|
|
|
|
public override void UnregisterInput(PlayerInput playerInput)
|
|
{
|
|
playerInput.currentActionMap.FindAction("Movement").performed -= OnMovement;
|
|
playerInput.currentActionMap.FindAction("Movement").canceled -= OnMovement;
|
|
}
|
|
}
|