Global Game Jam 2023
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.

118 lines
3.0 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.InputSystem;
  5. public class BoatController : MonoBehaviour
  6. {
  7. private Rigidbody rigidBody;
  8. [SerializeField]
  9. private float ForwardFactor = 1f;
  10. [SerializeField]
  11. private float RotationFactor = 1f;
  12. [SerializeField]
  13. private float BrakeFactor = 1f;
  14. [SerializeField]
  15. private float ObstacleBounce = 2f;
  16. [SerializeField]
  17. private bool m_usedebugkeys;
  18. [SerializeField]
  19. private AudioClip[] m_bonkClips;
  20. void Awake()
  21. {
  22. rigidBody = GetComponent<Rigidbody>();
  23. }
  24. // Update is called once per frame
  25. void Update()
  26. {
  27. if (!m_usedebugkeys)
  28. return;
  29. //get input
  30. if (Input.GetKeyDown(KeyCode.A))
  31. {
  32. RowLeft();
  33. }
  34. if (Input.GetKeyDown(KeyCode.D))
  35. {
  36. RowRight();
  37. }
  38. if (Input.GetKeyDown(KeyCode.S))
  39. {
  40. Brake();
  41. }
  42. }
  43. private void FixedUpdate()
  44. {
  45. //print(transform.rotation.eulerAngles.y);
  46. //constrain rotation
  47. if (transform.rotation.eulerAngles.y > 180 && transform.rotation.eulerAngles.y < 270)
  48. {
  49. print("under");
  50. transform.rotation = Quaternion.Euler(new Vector3(0f, 270f, 0f));
  51. rigidBody.angularVelocity = Vector3.zero;
  52. }
  53. //constrain rotation
  54. if (transform.rotation.eulerAngles.y < 180 && transform.rotation.eulerAngles.y > 90f)
  55. {
  56. print("over");
  57. transform.rotation = Quaternion.Euler(new Vector3(0f, 90f, 0f));
  58. rigidBody.angularVelocity = Vector3.zero;
  59. }
  60. }
  61. public void RowLeft()
  62. {
  63. rigidBody.AddForce(transform.forward * ForwardFactor, ForceMode.Acceleration);
  64. rigidBody.AddTorque(transform.up * -RotationFactor);
  65. }
  66. public void RowLeft(float velocity)
  67. {
  68. rigidBody.AddForce(transform.forward * ForwardFactor * velocity, ForceMode.Acceleration);
  69. rigidBody.AddTorque(transform.up * -RotationFactor * velocity);
  70. }
  71. public void RowRight()
  72. {
  73. rigidBody.AddForce(transform.forward * ForwardFactor, ForceMode.Acceleration);
  74. rigidBody.AddTorque(transform.up * RotationFactor);
  75. }
  76. public void RowRight(float velocity)
  77. {
  78. rigidBody.AddForce(transform.forward * ForwardFactor * velocity, ForceMode.Acceleration);
  79. rigidBody.AddTorque(transform.up * RotationFactor * velocity);
  80. }
  81. public void Brake()
  82. {
  83. rigidBody.AddForce(rigidBody.velocity * - BrakeFactor);
  84. rigidBody.AddTorque(rigidBody.angularVelocity * -BrakeFactor);
  85. }
  86. public void OnCollisionEnter(Collision collision)
  87. {
  88. if (collision.gameObject.CompareTag("Obstacle"))
  89. {
  90. Debug.Log($"Hitting: {collision.gameObject} Foce: {collision.impulse * ObstacleBounce}");
  91. rigidBody.AddForce(collision.impulse * ObstacleBounce, ForceMode.Impulse);
  92. SFXPlayer.Play(m_bonkClips);
  93. }
  94. }
  95. }