-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFishMovementScript.cs
91 lines (65 loc) · 2.55 KB
/
FishMovementScript.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FishMovementScript : MonoBehaviour {
public Rigidbody rb;
public Camera cam;
private float initXDiff, initZDiff;
public int time = 0;
private int t1, t2, t3, t4;
private Vector3 initPos;
private bool first = true;
void Awake()
{
// Fix clockwise or counter clockwise
/*if (Vector3.Distance(Vector3.zero, pathCenter) > Vector3.Distance(Vector3.zero, transform.position)){
transform.Rotate(Vector3.up, 180);
}*/
// Save initial x and z coordinates
}
void Start()
{
}
void Update()
{
if (first){
initXDiff = transform.position.x;
initZDiff = transform.position.z;
initPos = new Vector3(initXDiff, 0, initZDiff);
Debug.Log("INITIAL FISH POSITION: " + initPos);
Vector3 direction = new Vector3(initXDiff, 0, initZDiff);
Vector3 tangent = new Vector3(-initZDiff, 0, initXDiff);
// Position each fish tangent to its path circle
float angleChange = Vector3.Angle(Vector3.forward, tangent);
if (initZDiff > 0){
this.gameObject.transform.Rotate(Vector3.up, -angleChange);
} else {
this.gameObject.transform.Rotate(Vector3.up, angleChange);
}
//t1 = (int)Random.Range(1800, 2400); // die between 30-40 seconds
t1 = (int)Random.Range(1800, 2400);
t2 = t1 + 150; // roll over, beethoven
t3 = t1 + 240; // add force for 4 seconds after death
t4 = t3 + 360;
first = false;
}
rb = GetComponent<Rigidbody>();
if (time < t1){
float radius = Vector3.Distance(initPos, Vector3.zero);
float circumference = 2 * radius * Mathf.PI;
float rotationCoeff = Random.Range(0.25f, 2.0f);
float rotateBy = -(.5f) * (1 / 360) * circumference;
transform.Rotate(Vector3.up, (rotateBy * rotationCoeff));
float rotateAround = -0.5f;
transform.RotateAround(Vector3.zero, Vector3.up, (rotateAround * rotationCoeff));
} else if (time < t3){
if (time < t2){
transform.Rotate(Vector3.forward, 1.5f);
}
rb.AddForce(new Vector3(0, 2, 0));
} else if (time == t4){
Destroy(gameObject);
}
time++;
}
}