// Update is called once per frame void DoAIBehaviour() { Vector3 dir = Vector3.zero; //if we don't have a target pick one if (target == Vector3.zero) { PickRandPosition(); } else { //make a point on the same y plane as us Vector3 targ = new Vector3(target.x, transform.position.y, target.z); //if we have arrived at our target or we have no target then we pick another if (Vector3.Distance(targ, transform.position) < arriveRange) { PickRandPosition(); } else { //move towards the target dir = target - transform.position; } } //add to the list of steers float weight = 0.1f; WeightedSteer wd = new WeightedSteer(dir, weight); animalAI.desiredDirections.Add(wd); }
// Update is called once per frame void Update() { //seek a point 1000 units ahead of yourself Vector3 dir = Vector3.forward * 1000; WeightedSteer ws = new WeightedSteer(dir, weight); b.directions.Add(ws); }
// Update is called once per frame void DoAIBehaviour() { //if nothing to seek do nothing if (animalAI.foods.Count == 0) { return; } //if not hungry do nothing if (animalAI.a.hunger > hungerThreashoold) { return; } //holds the closest things to watch out for float record = Mathf.Infinity; GameObject r = null; //look for closest food to eat foreach (GameObject g in animalAI.foods) { //if creature is dead, move on if (g.GetComponent <Animal>().health <= 0) { continue; } float dist = Vector3.Distance(transform.position, g.transform.position); //find the closest one if (dist < record) { record = dist; r = g; } } //if within range to eat, then eat if (record < eatrange) { //make shure we don't overkill enemy //get maximum posible bite and apply float bite = Mathf.Clamp(biteDamage * Time.deltaTime, 0, r.GetComponent <Animal>().health); r.GetComponent <Animal>().health -= bite; animalAI.a.hunger += bite * foodGain; } else if (record != Mathf.Infinity) { //else move closer Vector3 dir = r.transform.position - this.transform.position; WeightedSteer wd = new WeightedSteer(dir, weight); animalAI.desiredDirections.Add(wd); } }
// Update is called once per frame void Update() { Rigidbody rb = leader.GetComponent <Rigidbody>(); //get target Vector3 target = leader.transform.position + rb.velocity; //calculate direction Vector3 dir = target - transform.position; float weight = 5; WeightedSteer ws = new WeightedSteer(dir, weight); b.directions.Add(ws); }
// Update is called once per frame void DoAIBehaviour() { //if you are hungry, don't care about player bool isAware; if (animalAI.a.health < 50) { isAware = false; } else { isAware = true; } //if nothing to fear do nothing if (animalAI.dangers.Count == 0) { return; } //holds the closest things to watch out for float record = Mathf.Infinity; GameObject r = null; //look for closest danger foreach (GameObject g in animalAI.dangers) { //Player ignore conditions if (g.name == "Player") { //get direction to player Vector3 d = g.transform.position - transform.position; //if sneaking, and you can't see them if (g.GetComponent <OVRPlayerController>().isCreeping&& Vector3.Dot(d, transform.forward) < visionThreashold) { //if we are aware don't let the player close if (Vector3.Distance(g.transform.position, transform.position) > innerRange) { continue; } else if (isAware == false) { continue; } } } else if (g.GetComponent <Animal>().health <= 0) //if animal is dead skip { continue; } float dist = Vector3.Distance(transform.position, g.transform.position); //find the closest one if (dist < record) { record = dist; r = g; } } //calculate weight based on distance from the danger float weight = 10f / (record * record); //move away Vector3 dir = Vector3.zero; if (r != null) { dir = this.transform.position - r.transform.position; } //add to the list of steers WeightedSteer wd = new WeightedSteer(dir, weight); animalAI.desiredDirections.Add(wd); }