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.

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