How do I make a cone of vision in raycast? Help!
Hello! I made a script from the vision of the enemy, the "field of vision" that actually worked !, the bad thing is that it is a line and it is very sub-realist, can I do the cone angle vision style? The most realistic enemy vision system. Thank you!!
using UnityEngine;
using System.Collections;
public class EnemyVision : MonoBehaviour {
public float Distance;
public Transform SightObject;
public bool Detected = false;
public GameObject Enemy;
void Start ()
{
}
void Update ()
{
RaycastHit hit;
if (Physics.Raycast(SightObject.transform.position, SightObject.transform.forward, out hit, Distance)) {
Debug.Log(hit.transform.name);
Debug.DrawRay(SightObject.transform.position, SightObject.transform.forward * Distance, Color.green);
if (hit.transform.tag == "Player")
{
Detected = true;
Enemy.gameObject.GetComponent<Enemy>().Distancia = Enemy.gameObject.GetComponent<Enemy>().DetectedDistance;
Enemy.gameObject.GetComponent<Enemy>().LastDistance = Enemy.gameObject.GetComponent<Enemy>().DetectedLastDistance;
}
}
}
}
here is a interesting and useful forum thread, with several approachs to solve this problem
Which?? So I already solve the question haha
Answer by xxmariofer · Jan 24, 2019 at 12:08 AM
Hello, sorry to not give you an answer in previous post but i didnt got notified, you cant easily do a cone with the rays, since you will have to instantitate a lot of rays in a circular direction and might also affect your performance, the easiest approach is to activate a object that works as a trigger, you can instantiate an object with a cone collider and do all that stuff in the ontriggerenter event, but you would have to refactor all your code, What was your problem from previous post? why wasnt detecting the player?
Hello! thanks for answering, before the player was not detected because the script contained inesary parameters that filled the code and blocked the features, now I started one from 0 and it worked. P.S: How do I make several raycast rays to form a cone? (flat). since I saw this several times and it's a good idea!
Your answer
Follow this Question
Related Questions
raycast going through walls 0 Answers
Raycast from evemy to player is way to far down 1 Answer
How do I check if an specific object is NOT inside a trigger? 0 Answers
How to account for a failed raycast? 1 Answer
Touch - Linecast collision detection 0 Answers