Hey!
I am developing a little shooter. To script the KI I’ve taken a Pyramid for FOV and I’m using the “Is Trigger”-Tool (or whatever it is) to trigger the Enemy, if the Player collides with
the Pyramid. When the Player collides with the Pyramid: first a Ray should be casted from the Enemy to the Player to detect if there’s something between Player and Enemy.
Then, if there’s nothing between them, the Enemy should chase the Player and attack him. Otherwise the Enemy shouldn’t do anything. So the problem is, that, when the Player is in the
Pyramid, the Enemy isn’t doing anything. I think he is not casting the Ray. Only, if the Player is in front of him he follows him. Hope you can help me. Here’s my Script… (Sorry for spelling mistakes) Btw It worked without the Raycast.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets;
using UnityEngine.AI;
public class EnemyAI : MonoBehaviour {
float distance;
public Transform target;
NavMeshAgent navComponent;
public GameObject Enemy;
void OnTriggerStay(Collider colInfo)
{
target = GameObject.FindGameObjectWithTag("Player").transform;
Ray ray = new Ray (Enemy.transform.position, target.position);
if (colInfo.tag == "Player")
{
RaycastHit hit;
if(Physics.Raycast (ray, out hit))
{
if (hit.collider.tag == "Player")
{
navComponent = Enemy.GetComponent<NavMeshAgent> ();
navComponent.SetDestination (target.position);
}
}
}
}
/*void lookAt()
{
Vector3 direction = (target.position - Enemy.transform.position).normalized;
Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
Enemy.transform.rotation = Quaternion.Slerp(Enemy.transform.rotation, lookRotation, Time.deltaTime * 5f);
}*/
}