Search Unity

Raycast Wrong Direction

Discussion in 'Scripting' started by BrickForSheep, Feb 4, 2013.

  1. BrickForSheep

    BrickForSheep

    Joined:
    Feb 4, 2013
    Posts:
    2
    Hey there.

    I'm working on setting up a basic AI pathing system. The first part of that system involves taking each node in the path and identifying which other nodes it can reach, then placing those into an array.

    The basic process for this is as follows:

    I identify the closest node to the current point. I set the current node to look at the target, then project a ray at that target. I then run a check to make sure the collider being hit by the ray is the same collider around the target node (i.e., making sure we don't hit anything in between). If it hits, the target node is put into an array of targets that the current node can reach, and we move on to the next closest target.

    The problem seems to be with the raycasting process. I seem to be successfully aiming at the target, but when the ray is cast, it doesn't point at the target. In checking the RaycastHit data, the collider being hit always seems to be drawn from the previous loop. When I aim at the first target, the hit registers as Null. When I aim at the second, I register a hit on the first, the third registers a hit on the second, and so on.

    Here's the update block I'm running. FindNearestNode() does exactly what it says, and seems to work fine.

    Any ideas as to what the problem might be here? I assume it's something to do with the order in which things are being called, but I don't know what to do to fix it.

    Thanks for any help.