- Home /
AI Player detect without having a target before detect.
Trying to figure out if a single ray can be fanned out. tying to get AI to detect player within a fov range. I have it on my player so that I can test it out before I transfer it. At the moment it only detects the line which is 1. to about only 0.8f. either side rotates. Am trying to make it where it will go to 0 on rotation or around 45 degrees on either. So far ray is lacking for this.
void SeePlayer() {
RaycastHit hit;
Ray seenRay = new Ray(Player.transform.localPosition, Player.transform.forward);
Debug.DrawRay(Player.transform.localPosition, Player.transform.forward * 10.0f, Color.red);
if(Physics.Raycast(seenRay, out hit, meleeAttackDistance))
{
if(hit.collider.tag == "Enemy")
{
if(Vector3.Distance(hit.transform.localPosition, Player.transform.localPosition) < 10.0f)
{
if(Vector3.Dot((hit.transform.localPosition - Player.transform.localPosition).normalized, Player.transform.forward) > 0)
{
Debug.Log("HIT");
}
}
}
}
}
Trying to use Vector3.Dot() to get it when an object moves into this FOV that it will know something is their to go attack it.
My AI will not have a player Selected as a Target yet, so i tried modifying this code.
if(Vector3.Distance(Target.transform.localPosition, Player.transform.localPosition) < 10.0f)
{
if(Vector3.Dot((Target.transform.localPosition - Player.transform.localPosition).normalized, Player.transform.forward) > 0) {
Debug.Log("Hit");
}
}
Is their a way without raycast to check if a collider with a rigidbody enters inside of the FOV cone without needing a collider object to detect with an OnTriggerEnter? Needs to be able to detect without knowing what its target could be before hand.
I have a similar problem. I haven't implemented it yet, but what I think might work is casting a cone of rays out at the correct angle of view, in this case 45 degrees. The greater the distance, the more rays you need to cast to keep things from slipping between, but I think it will work if the range is reasonable and the target isn't too small.
If you get a hit, then you can get the target position and just check that it stays within your 45° angle. I'll post my code if I get this to work.
EDIT:
Just came across this post, which also has some good info: http://answers.unity3d.com/questions/284685/regarding-the-efficiency-of-enemy-detection.html
Your answer
Follow this Question
Related Questions
Ai Targetting feet instead of my body 2 Answers
faster target selection 2 Answers
2d AI: enemy targeting c# 2 Answers
C# Targetting Help 1 Answer
How to determine a "cone of influence" used for targeting an object. 2 Answers