- Home /
Raycasting and foreach loops
I'm currently trying to write a piece of code that does something that is theoretically very simple. I have a list of walk points and scientists that I need to test if an alien (the game object where this script is attached) can see. I'm trying to use raycasting to determine this. here's the code I have so far:
public List<GameObject> test;
public List<GameObject> importantThingsToSee;
public List<GameObject> seeableScientists;
public List<GameObject> seeableWalkPoints;
public void Awake()
{
importantThingsToSee.AddRange(GameObject.FindGameObjectsWithTag("Scientist"));
importantThingsToSee.AddRange(GameObject.FindGameObjectsWithTag("Walk Point"));
PickANewPoint();
}
public void PickANewPoint()
{
seeableScientists.Clear();
seeableWalkPoints.Clear();
foreach (GameObject importantGameObject in importantThingsToSee)
{
Ray ray = new Ray(transform.position, transform.position - importantGameObject.transform.position);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
test.Add(hit.transform.gameObject);
foreach (GameObject compareImportantGameObject in importantThingsToSee)
{
Debug.Log(hit.transform.gameObject + " when compared to " + compareImportantGameObject);
if (GameObject.ReferenceEquals(hit.transform.gameObject, compareImportantGameObject) && compareImportantGameObject.tag == "Scientist")
{
Debug.Log("... is a successful scientist!");
seeableScientists.Add(compareImportantGameObject);
}
else if (GameObject.ReferenceEquals(hit.transform.gameObject, compareImportantGameObject) && compareImportantGameObject.tag == "Walk Point")
{
Debug.Log("... is a successful walk point!");
seeableWalkPoints.Add(compareImportantGameObject);
}
else
{
Debug.Log("... is a faliure.");
}
}
}
}
}
The code should work like this: I have one list of both the walk points and the scientists. When I run the game, that list gets filled correctly. Then it makes a ray and casts it to every gameobject in that list. At this point, I tried to compare the hit to the game object it was referencing at the time, but the raycast was out of sync with the foreach loop by the time that if statement was called. My "solution" was to put another foreach loop inside of the original and test the hit with every single game object in the importantThingsToSee list. In my scene there should be 6 objects it does a raycast for, but it's only doing 5, and at least 2 of those are going in what appears to be the exact opposite direction of where they're supposed to be going. Any help would be appreciated, thanks!
Answer by Bunny83 · May 03, 2020 at 10:21 AM
Uhm you know that vector subtraction create a vector in the form "tip - tail". So your raycast direction goes from the object you want to test towards your alien. However you start the ray at your alien object and therefore in the wrong direction.
I don't really understand your whole "out of sync" issue you're describing. I don't see any reason for your nested loop.
I would suggest something like that:
public void PickANewPoint()
{
seeableScientists.Clear();
seeableWalkPoints.Clear();
foreach (GameObject importantGameObject in importantThingsToSee)
{
Vector3 dir = importantGameObject.transform.position - transform.position;
Ray ray = new Ray(transform.position, dir);
RaycastHit hit;
if (!Physics.Raycast(ray, out hit))
continue; // we hit nothing? check next object
test.Add(hit.transform.gameObject);
if (hit.transform.gameObject != compareImportantGameObject)
continue; // the object we hit is not the object we want to test? check next object
if (compareImportantGameObject.CompareTag("Scientist"))
{
Debug.Log("... is a successful scientist!");
seeableScientists.Add(compareImportantGameObject);
}
else if (compareImportantGameObject.CompareTag("Walk Point"))
{
Debug.Log("... is a successful walk point!");
seeableWalkPoints.Add(compareImportantGameObject);
}
else
{
Debug.Log("... is a faliure.");
}
}
}
Note that the use of "continue" reduces the nesting depth and makes a clearer cut. Note that the reason you don't need the inner loop is that we iterate through all objects anyways. So if one object is hidden behind another it can not be seen since the raycast will always stop at the first object. So when you raycast against the object further away you will hit the closer one which of course is the wrong object. However when you later check the closer one you will detect the closer object properly. It should be obvious that you will never hit an object behind some other object.
If your / this approach is the right one depends on how you specify "can be seen". Since you just use a raycast against the objects pivots you essentially state that only when you have a clear sight along that line to the object, the object can be seen. If that's not what you intended you need a different approach. If you only want to make level geometry to be able to block the sight but not other object that you want to detect, you need a different approach.
A common solution for this is to first make sure all your relevant objects are on a seperate layer(s). More specifically all geometry that should be able to block the sight is on a layer. Instead of a Raycast you would simply use a Linecast between your alien and the object you want to test. However you would only cast agains the blocking geometry, not the actual objects you're interested in. If the line cast doesn't detect anything you have a clear sight to that object. If it does detect something it has to be blocking geometry and the sight is blocked.
Yep, that worked perfectly. I didn't realize I had my ray going backwards. The scene I had set up for this had the objects in a plus sign, so when the objects were getting added in the wrong order, I thought that the ray was lagging behind or something. Turns out it was just going backwards. Thank you!