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.3 KiB

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class BoatController : BuoyantObject {
  6. public float trunSpeed = 0.5f;
  7. public float breakSpeed = 20f;
  8. [Range(0.8f,1)]
  9. public float stopRatio = 0.95f;
  10. private bool applyBreak = true;
  11. // Use this for initialization
  12. void Start() {
  13. NotificationServer.register("switch off", switchOff);
  14. }
  15. void Update() {
  16. lookAtDir(Vector3.ProjectOnPlane(rb.velocity, Vector3.up));
  17. }
  18. void FixedUpdate() {
  19. doBreak();
  20. }
  21. // Update is called once per frame
  22. void lookAtDir(Vector3 inputDir) {
  23. if (inputDir == Vector3.zero)
  24. return;
  25. Quaternion targetRotation = Quaternion.LookRotation(inputDir, Vector3.up);
  26. float str = Mathf.Min(trunSpeed * Time.deltaTime, 1);
  27. transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, str);
  28. }
  29. void OnCollisionEnter(Collision collision) {
  30. if (collision.gameObject.CompareTag("Rock")) {
  31. float forwardHitSpeed = Vector3.Project(collision.relativeVelocity, transform.forward).magnitude;
  32. // Debug.Log("Forward hit:" + forwardHitSpeed);
  33. if (forwardHitSpeed > breakSpeed) {
  34. NotificationServer.notify("play sfx", "runAground:1");
  35. PlayerController.instance.takeOneDamage();
  36. }
  37. }
  38. }
  39. private void doBreak() {
  40. if (!applyBreak)
  41. return;
  42. Vector3 vel = rb.velocity;
  43. Vector3 flatVel = Vector3.ProjectOnPlane(vel, Vector3.up);
  44. flatVel *= stopRatio;
  45. flatVel.y = vel.y;
  46. rb.velocity = flatVel;
  47. }
  48. public override IEnumerator pushObject(Vector3 point, float power, float totalTime) {
  49. Vector3 dir = transform.position - point;
  50. dir = Vector3.ProjectOnPlane(dir, Vector3.up);
  51. dir.Normalize();
  52. float elapsedTime = 0;
  53. Vector3 force = dir * power;
  54. rb.AddForce(force,ForceMode.Impulse);
  55. while (elapsedTime < totalTime) {
  56. applyBreak = false;
  57. force = dir * power * Time.deltaTime;
  58. rb.AddForce(force);
  59. yield return new WaitForEndOfFrame();
  60. elapsedTime += Time.deltaTime;
  61. }
  62. applyBreak = true;
  63. }
  64. private void switchOff(object _)
  65. {
  66. rb.velocity = Vector3.zero;
  67. }
  68. }