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.

91 lines
2.2 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. }
  14. void Update() {
  15. lookAtDir(Vector3.ProjectOnPlane(rb.velocity, Vector3.up));
  16. }
  17. void FixedUpdate() {
  18. doBreak();
  19. }
  20. // Update is called once per frame
  21. void lookAtDir(Vector3 inputDir) {
  22. if (inputDir == Vector3.zero)
  23. return;
  24. Quaternion targetRotation = Quaternion.LookRotation(inputDir, Vector3.up);
  25. float str = Mathf.Min(trunSpeed * Time.deltaTime, 1);
  26. transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, str);
  27. }
  28. void OnCollisionEnter(Collision collision) {
  29. if (collision.gameObject.CompareTag("Rock")) {
  30. float forwardHitSpeed = Vector3.Project(collision.relativeVelocity, transform.forward).magnitude;
  31. // Debug.Log("Forward hit:" + forwardHitSpeed);
  32. if (forwardHitSpeed > breakSpeed) {
  33. NotificationServer.notify("play sfx", "runAground:1");
  34. PlayerController.instance.takeOneDamage();
  35. }
  36. }
  37. }
  38. private void doBreak() {
  39. if (!applyBreak)
  40. return;
  41. Vector3 vel = rb.velocity;
  42. Vector3 flatVel = Vector3.ProjectOnPlane(vel, Vector3.up);
  43. flatVel *= stopRatio;
  44. flatVel.y = vel.y;
  45. rb.velocity = flatVel;
  46. }
  47. public override IEnumerator pushObject(Vector3 point, float power, float totalTime) {
  48. Vector3 dir = transform.position - point;
  49. dir = Vector3.ProjectOnPlane(dir, Vector3.up);
  50. dir.Normalize();
  51. float elapsedTime = 0;
  52. Vector3 force = dir * power;
  53. rb.AddForce(force,ForceMode.Impulse);
  54. while (elapsedTime < totalTime) {
  55. applyBreak = false;
  56. force = dir * power * Time.deltaTime;
  57. rb.AddForce(force);
  58. yield return new WaitForEndOfFrame();
  59. elapsedTime += Time.deltaTime;
  60. }
  61. applyBreak = true;
  62. }
  63. }