void ApplyRules() { GameObject[] gos; gos = beeflock.allBee; //gos = globalflock.allBee; //goalPos Vector3 vcentre = Vector3.zero; //평균 중심점을 구하기 위한것 Vector3 vavoid = Vector3.zero; float gSpeed = 0.1f; Vector3 goalPos = beeflock.goalPos; //모든 군집 객체들을 가져온다. float dist; int groupsize = 0; foreach (GameObject go in gos) { if (go == null) { Debug.Log("빔"); } dist = Vector3.Distance(go.transform.position, this.transform.position); //나자신과 다른 군집개체와의 거리계산 if (dist <= neighbourDistance) //그거리가 내가 설정한 개체간 거리보다 작으면 { vcentre += go.transform.position; //더한다 groupsize++; //증가시켜줌 나중에 무언가 평균을 구할때 사용됨 if (dist < 1.0f) // 너무 작으면 간격을 유지시켜주기위해 { vavoid = vavoid + (this.transform.position - go.transform.position); } flock anotherFlock = go.GetComponent <flock>(); gSpeed = gSpeed + anotherFlock.speed; } } if (groupsize > 0) { vcentre = vcentre / groupsize + (goalPos = this.transform.position); speed = gSpeed / groupsize; Vector3 direction = (vcentre + vavoid) - transform.position;//군집 방향으로 비틈??? if (direction != Vector3.zero) { transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime); } } }
void ApplyRules() { GameObject[] gos; gos = globalFlock.allFish; //calculate the center of the group Vector3 vcentre = Vector3.zero; //and avoid hitting each other Vector3 vavoid = Vector3.zero; float gSpeed = 0.1f; Vector3 goalPos = globalFlock.goalPos; //distance variable float dist; //calculate the group size based on distance from neighbour //see linne 54 int groupSize = 0; foreach (GameObject go in gos) { if (go != this.gameObject) { dist = Vector3.Distance(go.transform.position, this.transform.position); //if distance is 2 the fish is part of a group if (dist <= neighbourDistance) { //add center and group size vcentre += go.transform.position; groupSize++; //if less than 1 oway from the group if (dist < 1.0f) { //we will go in another direction vavoid = vavoid + (this.transform.position + go.transform.position); } //calculate speed from flock script attached to neighbour fish and have a average speed for the group at line 74 flock anotherFlock = go.GetComponent <flock>(); gSpeed = gSpeed + anotherFlock.speed; } } } //if we are in agroup if (groupSize > 0) { //calc avg center and speed of group vcentre = vcentre / groupSize + (goalPos - this.transform.position); speed = gSpeed / groupSize; Vector3 direction = (vcentre + vavoid) - transform.position; //if direction is not = to 0 we will change direction if (direction != Vector3.zero) { //slowly turn from one rotation to another transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime); } } }
void Start() { flock = GetComponent <flock>(); changePopSize(0.5f); percentageInitial(0.5f); infectiveness(0.5f); survivibility(0.5f); simspeed(0.5f); changeRadius(0.5f); }
void ApplyRules() //applied the flocking rules { GameObject[] gos; //gos stands from game object gos = Ale_FishPlacement.allFish; //it needs to know the position of all the fish in the array Vector3 vcentre = Vector3.zero; Vector3 vavoid = Vector3.zero; //this is important for realism float gSpeed = 0.1f; //group speed Vector3 goalPos = Ale_FishPlacement.goalPos; //talking to the placement script float dist; int groupSize = 0; //based on who is within the neighbour distance //calculating group size foreach (GameObject go in gos) { if (go != this.gameObject) { //choosing neighbours dist = Vector3.Distance(go.transform.position, this.transform.position); if (dist <= neighbourDistance) { vcentre += go.transform.position; groupSize++; if (dist < 1.0f) // if distance is too small we want to avoid { vavoid = vavoid + (this.transform.position - go.transform.position); } //grabbing the flock script and using the speed of neighbouring fish and adding it to speed total for average speed flock anotherFlock = go.GetComponent <flock>(); gSpeed = gSpeed + anotherFlock.speed; } } } //if we are in a group then we can calculate the average centre of group and average speed if (groupSize > 0) { vcentre = vcentre / groupSize + (goalPos - this.transform.position); speed = gSpeed / groupSize; Vector3 direction = (vcentre + vavoid) - transform.position; // this will give us the direction the fish need to turn into if (direction != Vector3.zero) //if direction is not equal to 0 fish will change direction { transform.rotation = Quaternion.Slerp(transform.rotation, //slerp will slowly turn us from one direction to the new one Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime); } } }
void ApplyRules() { GameObject[] gos = new GameObject[gFlock.allFish.Count]; gFlock.allFish.CopyTo(gos); Vector3 vcentre = Vector3.zero; Vector3 vavoid = Vector3.zero; float gSpeed = 0.1f; Vector3 goalPos = globalFlock.goalPos; float dist; int groupSize = 0; foreach (GameObject go in gos) { if (go != this.gameObject) { dist = Vector3.Distance(go.transform.position, this.transform.position); if (dist <= neighbourDistance) { vcentre += go.transform.position; groupSize++; if (dist < 2.0f) { vavoid = vavoid + (this.transform.position - go.transform.position); } flock anotherFlock = go.GetComponent <flock>(); gSpeed = gSpeed + anotherFlock.speed; } } } if (groupSize > 0) { vcentre = vcentre / groupSize + (goalPos - this.transform.position); speed = gSpeed / groupSize; this.GetComponent <Animation>()["Motion"].speed = speed; Vector3 direction = (vcentre + vavoid) - transform.position; if (direction != Vector3.zero) { transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime); } } }
void flocking_rules() { GameObject[] group_pos; group_pos = global_flock.mygroup; Vector3 vcentre = global_flock.Position.position; Vector3 vavoid = Vector3.zero; float group_speed = 0.1f; Vector3 final_position = global_flock.final_position; float distance; int group_size = 0; foreach (GameObject mygo in group_pos) { if(mygo != this.gameObject) { distance = Vector3.Distance(mygo.transform.position, this.transform.position); if(distance <= neighbor_distance) { vcentre += mygo.transform.position; group_size++; if(distance < 1.0f) { vavoid = vavoid + (this.transform.position - mygo.transform.position); } flock another_object = mygo.GetComponent<flock>(); group_speed = group_speed + another_object.speed; } } } if (group_size > 0) { vcentre = vcentre / group_size + (final_position - this.transform.position); speed = group_speed / group_size; Vector3 direction = (vcentre + vavoid) - transform.position; if(direction != global_flock.Position.position) { transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(direction), rotation_speed * Time.deltaTime); } } }
void Rule() { GameObject[] gos; gos = globalFlock.allGO; Vector3 vCentre = Vector3.zero; Vector3 vAvoid = Vector3.zero; float gSpeed = 0.1f; Vector3 goalPos = globalFlock.goalPos; float dist; int groupSize = 0; foreach (GameObject go in gos) { if (go != gameObject) { dist = Vector3.Distance(go.transform.position, transform.position); if (dist <= myManager.distValue) { vCentre += go.transform.position; groupSize++; if (dist <= 2.0f) { vAvoid = vAvoid + (transform.position - go.transform.position); } flock anotherFlock = go.GetComponent <flock>(); gSpeed = gSpeed + anotherFlock.speed; } } } if (groupSize > 0) { vCentre = vCentre / groupSize + (goalPos - transform.position); speed = gSpeed / groupSize * speedMult; Vector3 direction = (vCentre + vAvoid) - transform.position; if (direction != Vector3.zero) { transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime); } } }
void ApplyRules() { GameObject[] gos; //gos = gameobjects gos = globalFlock.allFish; //The public static allfish array in globalflock script, this is here now so that each fish knows where all other fish are Vector3 vcentre = Vector3.zero; //Calculate the centre of the group Vector3 vavoid = Vector3.zero; //Avoidance vector for fish not to hit each other while trying to get to center of the group float gSpeed = 0.1f; //Group Speed Vector3 goalPos = globalFlock.goalPos; float dist; int groupSize = 0; foreach (GameObject go in gos) { if (go != this.gameObject) { dist = Vector3.Distance(go.transform.position, this.transform.position); if (dist <= neighbourDistance)//Group size is based on which fish are within the neighbour distance { vcentre += go.transform.position; groupSize++; if (dist < 1.0f)//If the fish get in a small distance they should avoid { vavoid = vavoid + (this.transform.position - go.transform.position); } flock anotherFlock = go.GetComponent <flock>(); gSpeed = gSpeed + anotherFlock.speed; } } } if (groupSize > 0) { vcentre = vcentre / groupSize + (goalPos - this.transform.position); speed = gSpeed / groupSize; Vector3 direction = (vcentre + vavoid) - transform.position; if (direction != Vector3.zero) { transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime); } } }
void ApplyRules() { GameObject[] gos; gos = globalFlock.allFish; //grab all the fish Vector3 vcentre = Vector3.zero; //centre of the group Vector3 vavoid = Vector3.zero; //avoid hitting fish in the middle float gSpeed = 0.1f; //Initial group speed. Vector3 goalPos = globalFlock.goalPos; float dist; // distance variable int groupSize = 0; //groupsize of the fish, who is within the neighbour distance foreach (GameObject go in gos) { if (go != this.gameObject) { dist = Vector3.Distance(go.transform.position, this.transform.position); if (dist <= neighbourDistance) // is fish part of the group? { vcentre += go.transform.position; //Add up the centres groupSize++; //Add up the groupsize if (dist < 1.0f) //if small distance avoid { vavoid = vavoid = (transform.transform.position - go.transform.position); //calculate vector away from fish } flock anotherFlock = go.GetComponent <flock>(); //grab flock script and add up speeds gSpeed = gSpeed + anotherFlock.speed; //group speed } } } if (groupSize > 0) //if we are in a group { vcentre = vcentre / groupSize + (goalPos - this.transform.position); //calculate the average centre speed = gSpeed / groupSize; //calculate the average speed Vector3 direction = (vcentre = vavoid) - transform.position; if (direction != Vector3.zero) { transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime); } } }
void ApplyRules() { GameObject[] gos; gos = fishglobal.allFish; //GameObject[] gos = GameObject.FindGameObjectsWithTag("Fish_separate"); float gSpeed = 1f; Vector3 vcenter = Vector3.zero; Vector3 vavoid = Vector3.zero; float dist; Vector3 goalPos = fishglobal.goalPos; int groupSize = 0; foreach (GameObject go in gos) { if (go != gameObject) { dist = Vector3.Distance(go.transform.position, this.transform.position); if (dist <= neighborDistance) { vcenter += go.transform.position; groupSize++; if (dist < 1.0f) { vavoid = vavoid + (this.transform.position - go.transform.position); } flock anotherFlock = go.GetComponent <flock>(); gSpeed = gSpeed + anotherFlock.speed; } } // if (groupSize > 0) { // vcenter = vcenter / groupSize + (goalPos - gameObject.transform.position); // speed = gSpeed / groupSize; // // Vector3 direction = (vcenter + vavoid) - transform.position; // // if (direction != Vector3.zero) { // transform.rotation = Quaternion.Slerp (transform.rotation, // Quaternion.LookRotation (direction), // rotationalSpeed); // } // } } }
// Update is called once per frame void Update() { transform.rotation = Quaternion.LookRotation(rb.velocity) * rot; ttl -= Time.deltaTime; if (ttl <= 0) { foreach (Transform child in this.transform) { flock otherScript = child.gameObject.GetComponent <flock>(); otherScript.reset(); } Destroy(this.gameObject); } }
// Apply Rules to boids function void ApplyRules() { GameObject[] gos; gos = globalFlock.boids; Vector3 vcenter = Vector3.zero; Vector3 vavoid = Vector3.zero; float groupSpeed = 0.1f; Vector3 goal = globalFlock.goal; float dist; int groupSize = 0; foreach (GameObject go in gos) { if (go != this.gameObject) { dist = Vector3.Distance(go.transform.position, this.transform.position); if (dist <= neighborDistance) { vcenter += go.transform.position; groupSize++; if (dist < 1.0f) { vavoid = vavoid + (this.transform.position - go.transform.position); } flock anotherFlock = go.GetComponent <flock>(); groupSpeed = groupSpeed + anotherFlock.speed; } } } if (groupSize > 0) { vcenter = vcenter / groupSize + (goal - this.transform.position); speed = groupSpeed / groupSize; Vector3 direction = (vcenter + vavoid) - transform.position; if (direction != Vector3.zero) { transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime); } } }
void ApplyRules() { GameObject[] gos; gos = gb_flock.allFish; Vector3 g_centre = Vector3.zero; Vector3 g_avoid = Vector3.zero; float g_speed = 0.1f; Vector3 goalPos = gb_flock.goalPos; float dist; int groupSize = 0; //团队规模数量 foreach (GameObject go in gos) //计算自己与每条鱼的距离 { if (go != this.gameObject && go != null) { dist = Vector3.Distance(go.transform.position, this.transform.position); if (dist <= neighbourDistance) //如果是在邻居范围就加入团队 { g_centre += go.transform.position; //计算总的团队中心 groupSize++; if (dist < 1.0f)//如果太近就计算出防撞向量 { g_avoid = g_avoid + (this.transform.position - go.transform.position); } flock antherFlock = go.GetComponent <flock>(); g_speed = g_speed + antherFlock.speed;//计算团队总速度 } } } if (groupSize > 0) { g_centre = g_centre / groupSize + (goalPos - this.transform.position); speed = g_speed / groupSize; Vector3 direction = (g_centre + g_avoid) - transform.position; if (direction != gb_flock.center.position) { transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime); } //使鱼平滑转向 } }
void ApplyR() { //flocking GameObject[] gos; gos = globalflock.allFish; Vector3 vcenter = Vector3.zero; Vector3 vavoid = Vector3.zero; float gSpeed = 0.1f; Vector3 goal = globalflock.goal; float dist; int Gsize = 0; //check against other fish foreach (GameObject go in gos) { if (go != this.gameObject) { dist = Vector3.Distance(go.transform.position, this.transform.position); if (dist <= neighbourDist) { vcenter += go.transform.position; Gsize++; if (dist < 1.0f) { vavoid = vavoid + (this.transform.position - go.transform.position); } flock anotherFlock = go.GetComponent <flock>(); gSpeed = gSpeed + anotherFlock.speed; } } } //set in the right direction if (Gsize > 0) { vcenter = vcenter / Gsize + (goal - this.transform.position); speed = gSpeed / Gsize; Vector3 direction = (vcenter + vavoid) - transform.position; if (direction != Vector3.zero) { transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(direction), roatationspeed * Time.deltaTime); } } }
void ApplyRules() { GameObject[] gos; gos = GlobalFlock.allFish; Vector3 ventre = Vector3.zero; Vector3 vavoid = Vector3.zero; float gSpeed = 0.1f; Vector3 goalPos = GlobalFlock.goalPos; float dist; int groupSize = 0; foreach (GameObject go in gos) { if (go != this.gameObject) { dist = Vector2.Distance(go.transform.position, this.transform.transform.position); if (dist <= neighbourDistance) { ventre += go.transform.position; groupSize++; if (dist < 1.0f) { vavoid = vavoid + (this.transform.position - go.transform.position); } flock antherFlock = go.GetComponent <flock> (); gSpeed = gSpeed + antherFlock.fishSpeed; } } } if (groupSize > 0) { ventre = ventre / groupSize + (goalPos - this.transform.position); fishSpeed = gSpeed / groupSize; Vector3 direction = (ventre + vavoid) - transform.position; if (direction != Vector3.zero) { transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime); } } }
// Update is called once per frame void Update() { float dt = Time.deltaTime; for (int i = 0; i < numFish; i++) { flock otherScript = allFish[i].GetComponent <flock>(); if (otherScript.hit) { break; } lifeTime[i] -= dt; if (lifeTime[i] <= 0) { lifeTime[i] = Random.Range(4, 7); allFish[i].transform.position = getRandomPos(); } } }
private bool inRange(StateController controller) { flock currentObjFlock = controller.currentObj.GetComponent <flock>(); List <GameObject> neighbors = controller.waveManager.GetComponent <levelController>().getNeighbours(controller.currentObj, currentObjFlock.neighbourRadius); foreach (GameObject n in neighbors) { if (n.GetComponent <flock>().playerFound == true) { controller.currentObj.GetComponent <flock>().isFlocking = false; break; } } if (Vector3.Distance(controller.Player.transform.position, controller.currentObj.transform.position) < playerRange)//add: or if zomb in neighborhood = player found { return(true); } else { return(false); } }
// Use this for initialization void Start() { boid = GetComponent <flock>(); }
void ApplyRules() { GameObject[] gos; gos = myManager.allFish; Vector3 vcentre = Vector3.zero; Vector3 vavoid = Vector3.zero; float gSpeed = 0.1f; Vector3 goalPos = myManager.allGoal[0].transform.position; float goalDis = Vector3.Distance(this.transform.position, goalPos); foreach (GameObject goal in myManager.allGoal) { float nextDis = Vector3.Distance(this.transform.position, goal.transform.position); if (nextDis <= goalDis) { goalPos = goal.transform.position; goalDis = nextDis; } } float dist; int groupSize = 0; foreach (GameObject go in gos) { if (go != this.gameObject) { dist = Vector3.Distance(go.transform.position, this.transform.position); if (dist <= neighbourDistance) { vcentre += go.transform.position; groupSize++; if (dist < 5.0f) { vavoid = vavoid + (this.transform.position - go.transform.position); } flock anotherFlock = go.GetComponent <flock>(); gSpeed = gSpeed + anotherFlock.speed; } } } if (groupSize > 0) { vcentre = vcentre / groupSize + (goalPos - this.transform.position); speed = gSpeed / groupSize; Vector3 direction = (vcentre + vavoid) - transform.position; if (direction != Vector3.zero) { transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime); } } }
void ApplyRules()//flock function { GameObject[] gos; gos = globalFlock.allFish;//can get all data from fish from globalFlock Vector3 hookPos = dropTheBait.hookPos; Vector3 nowPos = this.transform.position; float Range = Vector3.Distance(hookPos, nowPos); Vector3 vcentre = globalFlock.midPos; //get in the same way(center of the group) Vector3 vavoid = globalFlock.midPos; //avoid hitting float gSpeed = 0.05f; Vector3 goalPos = globalFlock.goalPos; Vector3 ndGoalPos = globalFlock.ndGoalPos; Vector3 baitPos = dropTheBait.baitPos; float dist; int groupSize = 0; foreach (GameObject go in gos) { if (go != this.gameObject) { dist = Vector3.Distance(go.transform.position, this.transform.position); if (dist <= neighbourDistance)//if <= u are in neighbour { vcentre += go.transform.position; groupSize++; if (dist < 0.2f)//too close avoid { vavoid = vavoid + (this.transform.position - go.transform.position); } flock anotherFlock = go.GetComponent <flock>(); gSpeed = gSpeed + anotherFlock.speed; } } } if (this.transform.position == baitPos) { baitCode.baitEnable = 0; } if (groupSize > 0) { if (baitCode.baitEnable == 1) { if (Random.Range(0, 10000) > 1000)//get baited { vcentre = vcentre / groupSize + (baitPos - this.transform.position); speed = gSpeed / groupSize; Vector3 direction = (vcentre + vavoid) - transform.position; if (direction != Vector3.zero)//turning { transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime); } /* if ((Range <= 0.2f) && (baitCode.baitEnable == 1)) * { * * baitCode.baitEnable = 0; * this.transform.position = hookPos; * fCatch = 1; * Debug.Log("Pbait"); * * }*/ } } else if (Random.Range(0, 10000) > 2500) { vcentre = vcentre / groupSize + (goalPos - this.transform.position); speed = gSpeed / groupSize; Vector3 direction = (vcentre + vavoid) - transform.position; if (direction != Vector3.zero)//turning { transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime); } } } }
void ApplyRules() { GameObject[] fishes; fishes = globalFlock.allFish; Vector3 goalPos = Vector3.zero; Vector3 vcentre = Vector3.zero; Vector3 vavoid = Vector3.zero; float gSpeed = 0.2f; if (id != 4) { goalPos = globalFlock.goalPrefabPositions [id].transform.position; } float dist; int groupSize = 0; foreach (GameObject fish in fishes) { if (fish != this.gameObject) { dist = Vector3.Distance(fish.transform.position, this.transform.position); if (id == fish.GetComponent <flock> ().id) { if (dist <= neighbourDistance) { vcentre += fish.transform.position; groupSize++; if (dist < 1.0f) { vavoid = vavoid + (this.transform.position - fish.transform.position); } flock anotherFlock = fish.GetComponent <flock> (); gSpeed = gSpeed + anotherFlock.speed; } } else { if ((fish.GetComponent <flock> ().id == 4 && dist < 7.0f) || (dist < 2.0f)) { vavoid = vavoid + (this.transform.position - fish.transform.position); } } } } if (groupSize > 0) { vcentre = vcentre / groupSize + (goalPos - this.transform.position); speed = gSpeed / groupSize; Vector3 direction = (vcentre + vavoid) - transform.position; if (direction != Vector3.zero) { transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime); } } }