Problems with Enemy AI with Raycasts
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);
}*/
}
Answer by HylosUnity · Mar 29, 2018 at 09:25 PM
In line one I meant AI :) ... For those guys who wanna know how I made it: There's is a function from Unity called "Linecast": https://docs.unity3d.com/ScriptReference/Physics.Linecast.html
So instead Raycast I used a Linecast.
Your answer
Follow this Question
Related Questions
How do I make AI objects not walk on top of each other? 5 Answers
how to check if an object is betwen enemy and player 1 Answer
HELP!! How to make my Mixamo Character sit on a CHAIR?? A* Pathfinding + Mixamo Animation 0 Answers
Can this script be better optimized? 1 Answer
I need help with AI,Force not working 0 Answers