|
|
- 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 void Awake()
- {
- m_input = GetComponent<PlayerInput>();
- m_controller = GetComponent<CharacterController>();
- }
-
-
- private void Update()
- {
- ApplyRotation();
- ApplyMovement();
- }
-
-
- 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);
- }
-
-
-
-
- }
|