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 GameObject m_child; private GameObject m_body; private void Awake() { m_input = GetComponent(); m_controller = GetComponent(); m_yeetController = GetComponent(); m_body = this.gameObject; } private void Update() { ApplyRotation(); ApplyMovement(); LockAxis(Vector3.up); } private void OnMovement(InputValue value) { Vector2 m_recievedInput = value.Get(); m_desiredDirection = new Vector3(m_recievedInput.x, 0.0f, m_recievedInput.y); } private void OnTriggerEnter(Collider collider) { Debug.Log("PlayerInputController.OnTriggerEnter: arrived"); m_child = collider.gameObject; } private void OnTriggerExit(Collider collider) { m_child = null; } private void OnYeet() { m_yeetController.parent = m_body; switch (m_yeetController.yeetState) { case YeetController.YeetState.Unheld: if(m_child) m_yeetController.Hold(m_child); // Grab nearest baby break; case YeetController.YeetState.Held: m_yeetController.Yeet(); // Yeet baby break; case YeetController.YeetState.Yeeting: // Cooldown? break; } } 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); } }