Some syntax correction on using an array of Rays inside a foreach for doing Raycasts
I know the title isn't the easiest to understand but that is roughly the gist. You understand how normally when you do the raycast check - if (physics.raycast( - You can use a pre-defined ray instead of typing out the position and direction?
Well I'm trying to do the same thing, but using a foreach loop to fire six different rays at once. At least that's the plan. Imagine checking on each side of a hexagon to see if there are any neighbours.
public Ray[] rays;
rays = new Ray[6];
rays[1] = new Ray(gameObject.transform.position, gameObject.transform.forward);
rays[2] = new Ray(gameObject.transform.position, (gameObject.transform.up * 60));
On a second note, I imagine I've done that 60 degree check incorrectly, but the point is, I'm trying to use an array of rays to simplify the checks and make a potential foreach out of them for the raycasting. Unfortunately it's impossible for me to memorise syntax, plus this is new territory for myself. This is what I've manged thus far.
foreach (Ray item in rays)
{
RaycastHit hit;
if (Physics.Raycast(rays, out hit, 80))
{
print(hit.collider.gameObject.name);
Debug.DrawRay(gameObject.transform.position, gameObject.transform.forward);
}
}
Simple enough logic. For every ray I've defined in that rays array, I want to use their values inside that raycast check. Unfortunately rays is incompatible.
I imagine there's a correct way to do this, but I don't know the required syntax. Any insight from a more seasoned coder would be highly appreciated.
All the best.
Answer by techniman · Sep 13, 2017 at 08:30 AM
It looks like you're iterating over an array, but then passing the entire array to the Raycast function. When using foreach, it will work through the contained code block once each time for each member of the array, giving you access to each individual item with (in this case) the Ray item
variable.
foreach (Ray item in rays) { ... if (Physics.Raycast(item, ...)) { ... } }
You can consult the Unity scripting reference if you have further trouble https://docs.unity3d.com/ScriptReference/Physics.Raycast.html
Use item. Got it. This seems to fix the problem about as concisely as I imagined. I have been using the API links but there's no example of them being used in arrays, so I was stuck in the mud on the syntax. On a final note, just to be sure it works. I don't think the second ray has the correct calculation on it to cast the ray out at a 60 degree angle from forwards (assu$$anonymous$$g the rotation axis to be y). Any idea what the correct equation for it is? And secondly, could I use item as the parameter for a draw ray, for debugging purposes?
I see; your current "rotation" attempt is simply amplifying the Vector3 by 60, basically pointing 60 units in the up direction. To get a rotation, you'll want to use either transform.Rotate() or maybe Vector3.RotateTowards() depending on what exactly you're looking to achieve. https://docs.unity3d.com/ScriptReference/Transform.Rotate.html https://docs.unity3d.com/ScriptReference/Vector3.RotateTowards.html Transform.Rotate could be useful, but it affects the transform directly rather than return a rotated transform, so you could make a copy of the GO's transform then rotate it by 60 degrees for each ray.
It looks like DrawRay takes Vector3, Vector3 rather than just a Ray, so you'll have to use: Debug.DrawRay(item.origin, item.direction)
to draw the ray. https://docs.unity3d.com/ScriptReference/Debug.DrawRay.html
Got the DrawRay set up. Ideally I want 6 rays feeling out from the centre of a hex tile gameobject, so at 0, 60, 120, 180, 240, and 300 degrees in y respectively. Just tried using the rotate in the ray itself,
HexRays[2] = new Ray(gameObject.transform.position, gameObject.transform.Rotate(0,0,60,Space.Self));
I get this likely isn't the right way, but I'm not sure how to convert the rotate into the Vector3 it asks for. It's a void at present. What should my syntax be?