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.

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