|
|
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class BoatController : BuoyantObject {
-
- public float trunSpeed = 0.5f;
- public float breakSpeed = 20f;
- [Range(0.8f,1)]
- public float stopRatio = 0.95f;
-
- private bool applyBreak = true;
-
-
- // Use this for initialization
- void Start() {
- NotificationServer.register("switch off", switchOff);
- }
-
- void Update() {
- lookAtDir(Vector3.ProjectOnPlane(rb.velocity, Vector3.up));
- }
-
- void FixedUpdate() {
-
- doBreak();
- }
-
- // Update is called once per frame
- void lookAtDir(Vector3 inputDir) {
- if (inputDir == Vector3.zero)
- return;
- Quaternion targetRotation = Quaternion.LookRotation(inputDir, Vector3.up);
- float str = Mathf.Min(trunSpeed * Time.deltaTime, 1);
- transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, str);
- }
-
- void OnCollisionEnter(Collision collision) {
- if (collision.gameObject.CompareTag("Rock")) {
-
- float forwardHitSpeed = Vector3.Project(collision.relativeVelocity, transform.forward).magnitude;
-
- // Debug.Log("Forward hit:" + forwardHitSpeed);
- if (forwardHitSpeed > breakSpeed) {
- NotificationServer.notify("play sfx", "runAground:1");
- PlayerController.instance.takeOneDamage();
- }
- }
- }
-
-
- private void doBreak() {
- if (!applyBreak)
- return;
-
- Vector3 vel = rb.velocity;
- Vector3 flatVel = Vector3.ProjectOnPlane(vel, Vector3.up);
-
- flatVel *= stopRatio;
- flatVel.y = vel.y;
- rb.velocity = flatVel;
- }
-
- public override IEnumerator pushObject(Vector3 point, float power, float totalTime) {
- Vector3 dir = transform.position - point;
- dir = Vector3.ProjectOnPlane(dir, Vector3.up);
- dir.Normalize();
-
- float elapsedTime = 0;
-
- Vector3 force = dir * power;
- rb.AddForce(force,ForceMode.Impulse);
-
- while (elapsedTime < totalTime) {
- applyBreak = false;
-
- force = dir * power * Time.deltaTime;
- rb.AddForce(force);
-
- yield return new WaitForEndOfFrame();
-
- elapsedTime += Time.deltaTime;
- }
- applyBreak = true;
- }
-
- private void switchOff(object _)
- {
- rb.velocity = Vector3.zero;
- }
-
-
- }
|