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.

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