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.

114 lines
2.9 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. void Awake()
  19. {
  20. rigidBody = GetComponent<Rigidbody>();
  21. }
  22. // Update is called once per frame
  23. void Update()
  24. {
  25. if (!m_usedebugkeys)
  26. return;
  27. //get input
  28. if (Input.GetKeyDown(KeyCode.A))
  29. {
  30. RowLeft();
  31. }
  32. if (Input.GetKeyDown(KeyCode.D))
  33. {
  34. RowRight();
  35. }
  36. if (Input.GetKeyDown(KeyCode.S))
  37. {
  38. Brake();
  39. }
  40. }
  41. private void FixedUpdate()
  42. {
  43. //print(transform.rotation.eulerAngles.y);
  44. //constrain rotation
  45. if (transform.rotation.eulerAngles.y > 180 && transform.rotation.eulerAngles.y < 270)
  46. {
  47. print("under");
  48. transform.rotation = Quaternion.Euler(new Vector3(0f, 270f, 0f));
  49. rigidBody.angularVelocity = Vector3.zero;
  50. }
  51. //constrain rotation
  52. if (transform.rotation.eulerAngles.y < 180 && transform.rotation.eulerAngles.y > 90f)
  53. {
  54. print("over");
  55. transform.rotation = Quaternion.Euler(new Vector3(0f, 90f, 0f));
  56. rigidBody.angularVelocity = Vector3.zero;
  57. }
  58. }
  59. public void RowLeft()
  60. {
  61. rigidBody.AddForce(transform.forward * ForwardFactor, ForceMode.Acceleration);
  62. rigidBody.AddTorque(transform.up * -RotationFactor);
  63. }
  64. public void RowLeft(float velocity)
  65. {
  66. rigidBody.AddForce(transform.forward * ForwardFactor * velocity, ForceMode.Acceleration);
  67. rigidBody.AddTorque(transform.up * -RotationFactor * velocity);
  68. }
  69. public void RowRight()
  70. {
  71. rigidBody.AddForce(transform.forward * ForwardFactor, ForceMode.Acceleration);
  72. rigidBody.AddTorque(transform.up * RotationFactor);
  73. }
  74. public void RowRight(float velocity)
  75. {
  76. rigidBody.AddForce(transform.forward * ForwardFactor * velocity, ForceMode.Acceleration);
  77. rigidBody.AddTorque(transform.up * RotationFactor * velocity);
  78. }
  79. public void Brake()
  80. {
  81. rigidBody.AddForce(rigidBody.velocity * - BrakeFactor);
  82. rigidBody.AddTorque(rigidBody.angularVelocity * -BrakeFactor);
  83. }
  84. public void OnCollisionEnter(Collision collision)
  85. {
  86. if (collision.gameObject.CompareTag("Obstacle"))
  87. {
  88. Debug.Log($"Hitting: {collision.gameObject} Foce: {collision.impulse * ObstacleBounce}");
  89. rigidBody.AddForce(collision.impulse * ObstacleBounce, ForceMode.Impulse);
  90. }
  91. }
  92. }