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*.transform.position);*

if (Distance <= MinDetectDistance)
{
rayDirection = WM.Survivors*.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”);
}
}
}
* }*
}

There is your problem:

rayDirection = WM.Survivors*.transform.position - transform.localPosition ;*

use
rayDirection = WM.Survivors*.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 :wink:

Problem solve, it would seem that the problem was the rigid body i had on the target