- Home /
enemy raycast to detect player
so im making a stealth game where the enemy will patrol around, enemy can see 110 angle in front of them, and the player need to avoid them and get to the finish point, player can hide behind object where the enemy cannot see. I have been looking for different solution but still nothing came up, need help with the script for raycasting.
So i have this code, but the agent does not move when the player is in sight.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class enemyControl : $$anonymous$$onoBehaviour
{
Ray enemyRay;
public Color rayColor;
RaycastHit rayHit;
bool follow;
public float sightDist;
private Nav$$anonymous$$eshAgent agent;
public GameObject him;
void Start()
{
agent = GetComponent<Nav$$anonymous$$eshAgent>();
him = GameObject.FindGameObjectWithTag("Player");
}
void Update()
{
//enemyRay = new Ray(transform.position, transform.forward * sightDist);
Debug.DrawRay(transform.position + Vector3.up, transform.forward* sightDist, rayColor);
Debug.DrawRay(transform.position + Vector3.up, (transform.forward + transform.right).normalized * sightDist, rayColor);
Debug.DrawRay(transform.position + Vector3.up, (transform.forward - transform.right).normalized * sightDist, rayColor);
if (Physics.Raycast(transform.position + Vector3.up, transform.forward, out rayHit, sightDist))
{
if (rayHit.collider.gameObject.tag == "player")
{
agent.SetDestination(him.transform.position);
him = rayHit.collider.gameObject;
}
}
if (Physics.Raycast(transform.position + Vector3.up, (transform.forward + transform.right).normalized, out rayHit, sightDist))
{
if (rayHit.collider.gameObject.tag == "player")
{
agent.SetDestination(him.transform.position);
him = rayHit.collider.gameObject;
}
}
if (Physics.Raycast(transform.position + Vector3.up, (transform.forward - transform.right).normalized, out rayHit, sightDist))
{
if (rayHit.collider.gameObject.tag == "player")
{
agent.SetDestination(him.transform.position);
him = rayHit.collider.gameObject;
}
}
}
void OnTriggerEnter(Collider coll)
{
if (coll.tag == "Player")
{
agent.SetDestination(him.transform.position);
him = coll.gameObject;
}
}
}
Answer by CherryPicker · Oct 19, 2018 at 09:05 AM
Here's what I would do.
Use an invisble cone object. When the cone collides with the player send a raycast towards him. If the player is hiding behind an object the raycast won't hit him otherwise it will
This way you only raycast when you need to, when the cone collides with the player. You can make the cone be 110 degrees.
that is a great idea, however im not familiar with raycast and the cone you mention, do you have a script with comments, so i can check it out?
Your answer
Follow this Question
Related Questions
Enemy raycast detection 0 Answers
How to stop enemy shooting through wall 1 Answer
multiple enemy AI raycast 1 Answer
Enemies should check if they are line of side to the player before attacking. 0 Answers
Enemy to move out of way of player 1 Answer