Currently I am Working On Game Based on A movie, In which player is stuck inside a maze and he has to find keys to unlock doors and solve sum puzzles and gt out of maze also he is followed my zombies. Every thing is ready accept Enemy. I want to Create An Enemy That moves Randomly in side the Maze , if Enemy Sees Player then it moves towards player and attack him or if Enemy Hears a gun Shot then it should Trace the aprrox. position of gun shot. This is My Enemy Script Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityStandardAssets.Characters.ThirdPerson; using UnityEngine.AI; public class ScanForPlayer : MonoBehaviour { public float viewDistance; public float viewAngle; public LayerMask viewMask; Transform player; bool playerIsVisible; public NavMeshAgent agent; public ThirdPersonCharacter character; void Start() { player = GameObject.FindWithTag("Player").transform; agent.updateRotation = false; } void Update() { playerIsVisible = CanSeePlayer (); if (agent.remainingDistance <= 1f || playerIsVisible || (Vector3.Distance (transform.position, FindObjectOfType<Shooting> ().FirePos) < viewDistance / 2 && FindObjectOfType<Shooting> ().isShooting)) { AgentMover (); } character.Move (agent.desiredVelocity, false, false); } bool CanSeePlayer() { if (Vector3.Distance(transform.position, player.position) < viewDistance) { Vector3 dirToPlayer = (player.position - transform.position).normalized; float angleBetweenGuardAndPlayer = Vector3.Angle(transform.forward, dirToPlayer); if (angleBetweenGuardAndPlayer < viewAngle / 2f) { if (!Physics.Linecast(transform.position, player.position, viewMask)) { return true; } } } return false; } Vector3 RandomNavSphere (Vector3 origin, float dist, int layermask) { Vector3 randDirection = Random.insideUnitSphere * dist; randDirection += origin; UnityEngine.AI.NavMeshHit navHit; UnityEngine.AI.NavMesh.SamplePosition (randDirection, out navHit, dist, layermask); if (Vector3.Distance (transform.position, FindObjectOfType<Shooting>().FirePos) < viewDistance/2 && FindObjectOfType<Shooting>().isShooting) { return FindObjectOfType<Shooting> ().FirePos; } return navHit.position; } void AgentMover () { if (agent.remainingDistance <= 1f || playerIsVisible || (Vector3.Distance (transform.position, FindObjectOfType<Shooting> ().FirePos) < viewDistance / 2 && FindObjectOfType<Shooting> ().isShooting) || agent.isStoppeds) { if (Vector3.Distance (transform.position, player.position) > 1) { if (playerIsVisible) { agent.SetDestination (player.position); } else { agent.SetDestination (RandomNavSphere (transform.position, viewDistance * 10, viewMask)); } } else { agent.isStopped = true; Debug.Log ("Daed"); } } } } here my player script Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityStandardAssets.Characters.ThirdPerson; using UnityEngine.AI; public class ScanForPlayer : MonoBehaviour { public float viewDistance; public float viewAngle; public LayerMask viewMask; Transform player; bool playerIsVisible; public NavMeshAgent agent; public ThirdPersonCharacter character; void Start() { player = GameObject.FindWithTag("Player").transform; agent.updateRotation = false; } void Update() { playerIsVisible = CanSeePlayer (); if (agent.remainingDistance <= 1f || playerIsVisible || (Vector3.Distance (transform.position, FindObjectOfType<Shooting> ().FirePos) < viewDistance / 2 && FindObjectOfType<Shooting> ().isShooting)) { AgentMover (); } character.Move (agent.desiredVelocity, false, false); } bool CanSeePlayer() { if (Vector3.Distance(transform.position, player.position) < viewDistance) { Vector3 dirToPlayer = (player.position - transform.position).normalized; float angleBetweenGuardAndPlayer = Vector3.Angle(transform.forward, dirToPlayer); if (angleBetweenGuardAndPlayer < viewAngle / 2f) { if (!Physics.Linecast(transform.position, player.position, viewMask)) { return true; } } } return false; } Vector3 RandomNavSphere (Vector3 origin, float dist, int layermask) { Vector3 randDirection = Random.insideUnitSphere * dist; randDirection += origin; UnityEngine.AI.NavMeshHit navHit; UnityEngine.AI.NavMesh.SamplePosition (randDirection, out navHit, dist, layermask); if (Vector3.Distance (transform.position, FindObjectOfType<Shooting>().FirePos) < viewDistance/2 && FindObjectOfType<Shooting>().isShooting) { return FindObjectOfType<Shooting> ().FirePos; } return navHit.position; } void AgentMover () { if (agent.remainingDistance <= 1f || playerIsVisible || (Vector3.Distance (transform.position, FindObjectOfType<Shooting> ().FirePos) < viewDistance / 2 && FindObjectOfType<Shooting> ().isShooting) || agent.isStoppeds) { if (Vector3.Distance (transform.position, player.position) > 1) { if (playerIsVisible) { agent.SetDestination (player.position); } else { agent.SetDestination (RandomNavSphere (transform.position, viewDistance * 10, viewMask)); } } else { agent.isStopped = true; Debug.Log ("Dead"); } } } }
yaaa, As enemy ai Doesn't seems to work properly, when it is near to player, it behaves strangely, it either completely stops(means if we move away from enemy it doesn't chase player anymore) or calculate new path(even if we don't move from place enemy would calculate new path and moves away).
Well, your AgentMover() method has a very long if statement with a whole bunch of conditions I don't have time today to figure out. But my guess is, that's probably where things are going wrong. Try breaking that up a bit, or introducing some local variables that capture what those conditions actually mean to you. And use Debug.Log to ensure that they are functioning correctly.
This. I like to put “bing” “bang” “boom” at every stage of the chain of events to find out which one is failing. Im sure you could use numbers, but where’s the fun in that?
Code (CSharp): if (Vector3.Distance (transform.position, player.position) > 1) { if (playerIsVisible) { agent.SetDestination (player.position); } else { agent.SetDestination (RandomNavSphere (transform.position, viewDistance * 10, viewMask)); } } else { agent.isStopped = true; Debug.Log ("Daed"); } Problem is with this.... Agent is either completely stops or keeps moving when he approaches the player
By the way, I have solved the problem.... But.... Now I encountered another.... Playing different animation. Any idea how to use animator with my enemy script