- Home /
help with not working AI system
hello!
i have some trouble to trying to build a simple Ai system where the NPC is Run away from the player if it's facing him otherwise it will try to go to the player position to eat him
but it doesn't work as i want, it will run away but will freeze to the corner of map, and will not go to eat player if it's not facing him... what i'm missing up?
using UnityEngine;
using System.Collections;
public class TunAway : MonoBehaviour {
private Transform player;
private UnityEngine.AI.NavMeshAgent myNMagent;
private float nextTurnTime;
private Transform startTransform;
public float multiplyBy;
// Use this for initialization
void Start () {
player = GameObject.FindGameObjectWithTag ("Player").transform;
myNMagent = this.GetComponent<UnityEngine.AI.NavMeshAgent> ();
RunFrom ();
}
// Update is called once per frame
void Update () {
//if is not facing got to eat him
if(!ISFacing())
{
GotToEat();
Debug.Log("is not facing here");
}
else
{
RunFrom ();
}
}
public void RunFrom()
{
startTransform = transform;
transform.rotation = Quaternion.LookRotation(transform.position - player.position);
Vector3 runTo = transform.position + transform.forward * multiplyBy;
UnityEngine.AI.NavMeshHit hit;
UnityEngine.AI.NavMesh.SamplePosition(runTo, out hit, 20, 1 << UnityEngine.AI.NavMesh.GetAreaFromName("Walkable"));
transform.position = startTransform.position;
transform.rotation = startTransform.rotation;
myNMagent.SetDestination(hit.position);
}
private void GotToEat()
{
myNMagent.SetDestination(player.transform.position);
}
private bool ISFacing()
{
float dot = Vector3.Dot(transform.position,(player.position - transform.position).normalized);
if( dot < 0.79f)
{
return true;
}
else
return false;
}
}
Answer by Kougasama · May 18, 2019 at 03:47 PM
You might want to check if hit actually is something other than null. Other than that I can't see anything wrong either. Too bad navmesh doesn't automatically get the closest point walkable.. had the very same issue too, with npc getting stuck in corners. No matter what corners.
If it's just the map's end where they get stuck, maybe make them turn left or right when they reach there and no hit on walkable navmesh is detected.
yes, well i wasn't able to fix it ye.. same for the gotoeat function.. sometimes work sometimes not, i just change it witch compare angle function ins$$anonymous$$d dot product
using UnityEngine;
using System.Collections;
using UnityEngine.AI;
public class TunAway : $$anonymous$$onoBehaviour {
private Transform player;
private UnityEngine.AI.Nav$$anonymous$$eshAgent myN$$anonymous$$agent;
private float nextTurnTime;
private Transform startTransform;
public float multiplyBy;
// Use this for initialization
void Start () {
player = GameObject.FindGameObjectWithTag ("Player").transform;
myN$$anonymous$$agent = this.GetComponent<UnityEngine.AI.Nav$$anonymous$$eshAgent> ();
RunFrom ();
}
// Update is called once per frame
void Update () {
//if is not facing got to eat him
if(!ISFacing())
{
GotToEat();
Debug.Log("is not facing here");
}
else
{
RunFrom ();
}
}
public void RunFrom()
{
startTransform = transform;
transform.rotation = Quaternion.LookRotation(transform.position - player.position);
Vector3 runTo = transform.position + transform.forward * multiplyBy;
Nav$$anonymous$$eshHit hit;
if(Nav$$anonymous$$esh.SamplePosition(runTo, out hit, 50, 1 << Nav$$anonymous$$esh.GetAreaFromName("Walkable")))
{
transform.position = startTransform.position;
transform.rotation = startTransform.rotation;
myN$$anonymous$$agent.SetDestination(hit.position);
}
}
private void GotToEat()
{
transform.rotation = Quaternion.LookRotation(player.position - transform.position);
myN$$anonymous$$agent.SetDestination(player.transform.position);
}
private bool ISFacing()
{
float angle = 45;
float angleToPlayer = Vector3.Angle(player.transform.forward, transform.position - player.transform.position);
if( angleToPlayer < angle)
{
return true;
}
else
return false;
}
}
Answer by Sylon87 · May 19, 2019 at 01:20 AM
when i try to check if is hitting something with this `
bool RandomPoint(float range, out Vector3 result)
{
for (int i = 0; i < 30; i++)
{
Vector3 fixedPoint = transform.position + transform.forward * multiplyBy;
NavMeshHit hit;
if (NavMesh.SamplePosition(fixedPoint, out hit, 10.0f,1 << NavMesh.GetAreaFromName("Walkable")))
{
result = hit.position;
return true;
}
}
result = Vector3.zero;
return false;
}
it will return always true also if is on corners and is facing forward
Your answer
Follow this Question
Related Questions
How do I stop an Enemy NavMeshAgent from intersecting the player model? 3 Answers
Why doesn't my AI collide with walls? 0 Answers
NavMesh run away in enclosed space 0 Answers
NavMeshAgent not moving 0 Answers