override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { // realistic idle currentStep++; if (currentStep == frequency) { currentStep = 0; float angle = Random.Range(-turnRate, turnRate); wolfTransform.Rotate(Vector3.up, angle); wolfAgent.SetDestination(wolfTransform.position + wolfTransform.forward * movementDistance); Debug.DrawLine(wolfTransform.position, wolfAgent.destination, Color.blue); } List <GameObject> visionObjects = visionRadius.GetAllColliders("Sheep"); if (visionObjects.Count > 0) { if (visionObjects.Count >= numberOfSheepsToFlee) { animator.SetBool("isIdling", false); animator.SetBool("isFleeing", true); } else { animator.SetBool("isIdling", false); animator.SetBool("isChasing", true); } } }
override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { if (Time.timeScale != 0) { // detect enemy List <GameObject> wolves = visionRadius.GetAllColliders("Player"); if (wolves.Count > 0) { Vector3 meanPosition = Vector3.zero; foreach (GameObject go in wolves) { meanPosition += go.transform.position; } meanPosition /= wolves.Count; Vector3 oppositePosition = sheepTransform.position + (sheepTransform.position - meanPosition); if (sheepAgent.isActiveAndEnabled) { sheepAgent.SetDestination(oppositePosition); } Debug.DrawLine(sheepTransform.position, oppositePosition, Color.magenta); } else { animator.SetBool("isFleeing", false); animator.SetBool("isIdling", true); } } }
override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { // detect enemy List <GameObject> enemies = visionRadius.GetAllColliders("Sheep"); if (enemies.Count > 0) { Vector3 meanPosition = Vector3.zero; foreach (GameObject go in enemies) { meanPosition += go.transform.position; } meanPosition /= enemies.Count; Vector3 oppositePosition = wolfTransform.position + (wolfTransform.position - meanPosition); wolfAgent.SetDestination(oppositePosition); Debug.DrawLine(wolfTransform.position, oppositePosition, Color.magenta); } else { animator.SetBool("isFleeing", false); animator.SetBool("isIdling", true); } }
override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { if (Time.timeScale != 0) { // realistic idle currentStep++; if (currentStep == frequency) { currentStep = 0; float angle = Random.Range(-turnRate, turnRate); sheepTransform.Rotate(Vector3.up, angle); if (sheepAgent.isActiveAndEnabled) { sheepAgent.SetDestination(sheepTransform.position + sheepTransform.forward * movementDistance); } Debug.DrawLine(sheepTransform.position, sheepAgent.destination, Color.blue); } // if a wolf in vision, flee List <GameObject> wolfInVision = visionRadius.GetAllColliders("Player"); if (wolfInVision.Count > 0) { animator.SetBool("isIdling", false); animator.SetBool("isFleeing", true); } else { // if only itself in medium, wander List <GameObject> sheepsInMedium = mediumRadius.GetAllColliders("Sheep"); if (sheepsInMedium.Count == 1) { animator.SetBool("isIdling", false); animator.SetBool("isWandering", true); } else { // if less than nbSheepsToFlock in close, follow List <GameObject> sheepsInClose = closeRadius.GetAllColliders("Sheep"); if (sheepsInClose.Count <= numberOfSheepsToFlock && sheepsInClose.Count > 1) { animator.SetBool("isIdling", false); animator.SetBool("isFollowing", true); } } } } }
override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { // if more than the number in medium radius, flee, else chase the closest to kill List <GameObject> visionObjects = visionRadius.GetAllColliders("Sheep"); if (visionObjects.Count > 0) { GameObject closestSheep = null; float distance = float.MaxValue; float temp = 0; foreach (GameObject go in visionObjects) { temp = Vector3.Distance(wolfTransform.position, go.transform.position); if (temp < distance) { distance = temp; closestSheep = go; } } if (closestSheep != null) { wolfAgent.SetDestination(closestSheep.transform.position); Debug.DrawRay(wolfTransform.position, wolfAgent.destination, Color.red); if (closestSheep.transform.position == wolfTransform.position) { //considered dead animator.SetBool("isChasing", false); animator.SetBool("isIdling", true); } } // if the number of sheeps is facing it, flee int nbOfSheepsFacing = 0; foreach (GameObject go in visionObjects) { if (Vector3.Angle(go.transform.forward, wolfTransform.position - go.transform.position) < angle) { nbOfSheepsFacing++; } } if (nbOfSheepsFacing >= numberOfSheepsToFlee) { animator.SetBool("isChasing", false); animator.SetBool("isFleeing", true); } } else { animator.SetBool("isChasing", false); animator.SetBool("isIdling", true); } }
override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { //immobilised, will face mean pos of enemies in range List <GameObject> visionObjects = visionRadius.GetAllColliders("Enemy"); if (visionObjects.Count > 0) { Vector3 meanPos = Vector3.zero; foreach (GameObject go in visionObjects) { meanPos += go.transform.position; } meanPos /= visionObjects.Count; sheepTransform.LookAt(meanPos); //TODO: lerp Debug.DrawLine(sheepTransform.position, meanPos, Color.magenta); } }
override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { if (currentTimer % decisionTimer == 0) { List <GameObject> visionObjects = visionRadius.GetAllColliders("Sheep"); if (visionObjects.Count > 1) { List <GameObject> mediumObjects = mediumRadius.GetAllColliders("Sheep"); List <GameObject> closeObjects = closeRadius.GetAllColliders("Sheep"); // mean position from every sheeps in vision // weighted position depending on radius Vector3 meanPos = Vector3.zero; int nbClose = 0; int nbMedium = 0; int nbVision = 0; foreach (GameObject go in visionObjects) { if (go != sheepTransform.gameObject) { if (go.GetComponentInChildren <SheepController>().IsFollowingOrder()) { Debug.Log("FOLLOWING " + go.name); //prioritize the sheep following an order meanPos += go.transform.position; nbVision++; break; } else { if (closeObjects.Contains(go)) { for (int i = 0; i < closeWeight; i++) { meanPos += (go.transform.position); nbClose++; } } else if (mediumObjects.Contains(go)) { for (int i = 0; i < mediumWeight; i++) { meanPos += (go.transform.position); nbMedium++; } } else { for (int i = 0; i < visionWeight; i++) { meanPos += (go.transform.position); nbVision++; } } } } } int total = nbClose + nbMedium + nbVision; Debug.Log("follow: " + nbClose + " " + nbMedium + " " + nbVision); if (total > 0) { meanPos /= (nbClose + nbMedium + nbVision); if (sheepAgent.isActiveAndEnabled) { sheepAgent.SetDestination(meanPos); } Debug.DrawLine(sheepTransform.position, sheepAgent.destination, Color.green); currentTimer = 0; } } } currentTimer++; }
override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { if (Time.timeScale != 0) { if (currentTimer % decisionTimer == 0) { List <GameObject> sheepsInVision = visionRadius.GetAllColliders("Sheep"); if (sheepsInVision.Count > 1) { // mean position from every sheeps in vision // weighted position depending on radius Vector3 meanPos = Vector3.zero; foreach (GameObject go in sheepsInVision) { meanPos += go.transform.position; } meanPos /= sheepsInVision.Count; // prevent the case were only one is in close range List <GameObject> closeSheeps = closeRadius.GetAllColliders("Sheep"); if (closeSheeps.Count == 2) { animator.SetBool("isFollowing", false); animator.SetBool("isIdling", true); } else { if (sheepAgent.isActiveAndEnabled) { sheepAgent.SetDestination(meanPos); } Debug.DrawLine(sheepTransform.position, sheepAgent.destination, Color.green); } currentTimer = 0; } } // if a wolf in vision, flee List <GameObject> wolfInVision = visionRadius.GetAllColliders("Player"); if (wolfInVision.Count > 0) { animator.SetBool("isFollowing", false); animator.SetBool("isFleeing", true); } else { // if enough sheeps in close, go to idle List <GameObject> closeSheeps = closeRadius.GetAllColliders("Sheep"); if (closeSheeps.Count > nbSheepsToIdle) { animator.SetBool("isFollowing", false); animator.SetBool("isIdling", true); } else { // if no sheep in medium, go to wander List <GameObject> mediumSheeps = mediumRadius.GetAllColliders("Sheep"); if (mediumSheeps.Count == 1) { animator.SetBool("isFollowing", false); animator.SetBool("isWandering", true); } } } currentTimer++; } }