Global Game Jam 2021
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.6 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.InputSystem;
  5. [RequireComponent(typeof(UnityEngine.InputSystem.PlayerInput), typeof(CharacterController))]
  6. public class PlayerInputController : MonoBehaviour
  7. {
  8. [SerializeField]
  9. private float m_playerSpeed = 1;
  10. [SerializeField]
  11. private float m_rotationSpeed = 1;
  12. [SerializeField]
  13. [Tooltip("Used to spin on spot\n"
  14. + "0 = back\n"
  15. + "0.5 = left/right\n"
  16. + "1.0 = forward")]
  17. private AnimationCurve m_turnRadius;
  18. private UnityEngine.InputSystem.PlayerInput m_input;
  19. private CharacterController m_controller;
  20. private Vector3 m_desiredDirection;
  21. private bool m_recievedInput => m_desiredDirection.magnitude != 0;
  22. private YeetController m_yeetController;
  23. public GameObject testChild;
  24. public GameObject body;
  25. private void Awake()
  26. {
  27. m_input = GetComponent<UnityEngine.InputSystem.PlayerInput>();
  28. m_controller = GetComponent<CharacterController>();
  29. m_yeetController = GetComponent<YeetController>();
  30. }
  31. private void Update()
  32. {
  33. ApplyRotation();
  34. ApplyMovement();
  35. LockAxis(Vector3.up);
  36. }
  37. private void OnMovement(InputValue value)
  38. {
  39. Vector2 m_recievedInput = value.Get<Vector2>();
  40. m_desiredDirection = new Vector3(m_recievedInput.x, 0.0f, m_recievedInput.y);
  41. }
  42. private void OnYeet()
  43. {
  44. m_yeetController.parent = body;
  45. switch (m_yeetController.yeetState)
  46. {
  47. case YeetController.YeetState.Unheld:
  48. m_yeetController.Hold(testChild);
  49. // Grab nearest baby
  50. break;
  51. case YeetController.YeetState.Held:
  52. m_yeetController.Yeet();
  53. // Yeet baby
  54. break;
  55. case YeetController.YeetState.Yeeting:
  56. // Cooldown?
  57. break;
  58. }
  59. }
  60. private void ApplyRotation()
  61. {
  62. transform.forward = Vector3.Slerp(transform.forward, m_desiredDirection.normalized, m_rotationSpeed * Time.deltaTime);
  63. }
  64. private void ApplyMovement()
  65. {
  66. if (!m_recievedInput)
  67. return;
  68. float forwardRatio = (Vector3.Dot(transform.forward, m_desiredDirection.normalized) + 1) / 2;
  69. float speed = m_turnRadius.Evaluate(forwardRatio) * m_playerSpeed;
  70. m_controller.Move(transform.forward * speed * Time.deltaTime);
  71. }
  72. private void LockAxis(Vector3 axis)
  73. {
  74. transform.position = Vector3.ProjectOnPlane(transform.position, axis);
  75. }
  76. }