- Home /
having trouble with sight detection
I'm working on a game where the mob can detect by sight, sound and smell, as of right now I'm having a trouble with sight. The mob is aware of all of it intended targets and will pick one that is the closest to it and will try and see. the problem that I seem to be having is that the ray keep moving downward for some reason and I can figure out why.
void FindTargetBySight()
{
WorldManager WM = GameObject.Find("Main Camera").GetComponent("WorldManager") as WorldManager;
AstarAI ASAI = this.gameObject.GetComponent("AstarAI") as AstarAI;
for (int i = 0; i < WM.Survivors.Count; i++)
{
Distance = Vector3.Distance(this.gameObject.transform.position, WM.Survivors[i].transform.position);
if (Distance <= MinDetectDistance)
{
rayDirection = WM.Survivors[i].transform.position - transform.localPosition ;
RaycastHit DetectedTarget;
if (Physics.Raycast(transform.position, rayDirection, out DetectedTarget,MaxDistance))
{
Debug.DrawRay(transform.position, rayDirection);
if ( DetectedTarget.transform.tag == "Player")
{
Debug.Log("i see you");
}
}
}
}
}
$$anonymous$$Ob= monster, i and i did three times already. i don't know why it keep posting it like that
Answer by hathol · Sep 25, 2012 at 03:11 PM
There is your problem:
rayDirection = WM.Survivors[i].transform.position - transform.localPosition ;
use
rayDirection = WM.Survivors[i].transform.position - transform.position ;
instead.
If you're using different coordinate systems in one statement (target world space and object local space in this case) there is usually something wrong ;)
the rays still keeps moving downward even with the change.
Answer by Sinperhezine · Sep 25, 2012 at 07:10 PM
Problem solve, it would seem that the problem was the rigid body i had on the target
Your answer
Follow this Question
Related Questions
How to create an aura with selective collision detection 2 Answers
How can I compare a Distance i measured using Vector3.Distance with a number 2 Answers
enemy waypoints and detection scipting 2 Answers
how to identify near objects 1 Answer
How to use proximity detector to detect objects coming closer using C# script 1 Answer