ATC522-011\IGDA 7 years ago
parent
commit
5321263c9f
2 changed files with 58 additions and 0 deletions
  1. +46
    -0
      IronToad_UnityProject/Assets/Scripts/BoatController.cs
  2. +12
    -0
      IronToad_UnityProject/Assets/Scripts/DontRotate.cs

+ 46
- 0
IronToad_UnityProject/Assets/Scripts/BoatController.cs View File

@ -7,6 +7,10 @@ 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() {
@ -17,6 +21,10 @@ public class BoatController : BuoyantObject {
lookAtDir(Vector3.ProjectOnPlane(rb.velocity, Vector3.up));
}
void FixedUpdate() {
doBreak();
}
// Update is called once per frame
void lookAtDir(Vector3 inputDir) {
@ -38,4 +46,42 @@ public class BoatController : BuoyantObject {
}
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;
}
}

+ 12
- 0
IronToad_UnityProject/Assets/Scripts/DontRotate.cs View File

@ -5,6 +5,11 @@ using UnityEngine;
public class DontRotate : MonoBehaviour {
private Quaternion originalRot;
public float maxAngle;
public float turnSpeed = 0.5f;
// Use this for initialization
void Start () {
originalRot = transform.rotation;
@ -14,4 +19,11 @@ public class DontRotate : MonoBehaviour {
void Update () {
transform.rotation = originalRot;
}
// Update is called once per frame
void lookAtDir(Vector3 inputDir) {
Quaternion targetRotation = Quaternion.LookRotation(inputDir, Vector3.up);
float str = Mathf.Min(turnSpeed * Time.deltaTime, 1);
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, str);
}
}

Loading…
Cancel
Save