Hi there,
currently i am working on a game which depends on raycasting.
The most tasks work just fine, but now i hit a problem which i don’t get the correct idea.
What i want is, to cast a ray with a defined length (via Vector3.Magnitude / Vector3.SqrtMagnituded)
when i am using this, i RaycastAll won’t return any gameobjects, without it, i get more then i want, but not what i want.
Basically i tried the different signatures of Physics.Raycast and Physics.RaycastAll. with the “All” variant i tried to define the lengtht (by magnitued, testwise hardcoded length) but with the length, raycastall returns as i said 0 RaycastInfo’s.
please have a look at the paint picture, its a visualized description of my problem.
all the gameobjects have the length of “1”, therefore when i try to reach a field which is “2 up and 1 right”, i try to cast to direction Vector3.up*2 + Vector.right = rightUp; and then to get the lenght i call Vector3.Magnitude(rightUp) (which doesn’t work well).
any ideas how to get the correct gameobject with a defined lenght and the correct direction? even when i code it to “vector3up + vector3.right” and give it the calculated length (~ 2.5 or max 3…) raycastall returns nothing…
what works is, when the field is direct at the near of the origin field, then everything works…
any ideas? questions on my description? greetings
tony
Can you post some code?
In the mean time, the 4th parameter of Physics.Raycast decides the distance. The second vector is purely for direction.
Attempting to find something “2 up and 1 right” is only going to be possible if there’s a direct line of sight from your origin to the point. You can’t bend the line, sort of speak. For that, you’d have to cast two lines, first 2 up and then 1 to the right.
1 Like
unity is currently updating i’ll post the code later on.
well, i’ll try the 2 lines (2 up first, then 1 right)…
on the other hand, the direction can be anywhere. so i could make “Vector3.up + Vector3.right”, which should be a vector pointing to “North east” (in numbers 1,0,1), then it should theoretically work to find an exact gameobject in that direction.
as i said after the update ill post some code.
ah before i post the code: on other objects the raycasting to up+right is working just fine, but only because there i need only the “neighbours”.
okay, so here is the code:
rayCastOrigin: the origin point
direction: the direciton (can be all directions up, right, forward, back, up+right, up+left ect…)
raycasthit: the hitinfo
bool RayCastCheckFieldLayer(Vector3 rayCastOrigin, out RaycastHit hit, Vector3 direction)
{
RaycastHit[ ] hitCombos = null;
Ray ray = new Ray(rayCastOrigin, direction);
hitCombos = Physics.RaycastAll(rayCastOrigin, direction, 1 << 9); // 1 << 9 is a layer (to select only “them”)
// hitCombos = Physics.RaycastAll(rayCastOrigin, direction, Vector3.Magnitude, 1 << 9);
//hitCombos = Physics.RaycastAll(rayCastOrigin, direction);
//hitCombos = Physics.RaycastAll(ray);
// this return - logic is only testwise. later it will return either from the array or from the Raycast(…)
foreach(var r in hitCombos)
{
Debug.Log(r.collider.gameObject.name);
}
/* Debug.Log((hitCombos == null ? "was empty " : hitCombos.Length.ToString()));
if(hitCombos.Length > 0) // testwise
{
hit = hitCombos[hitCombos.Length-1];
return true;
}*/
return Physics.Raycast(rayCastOrigin, direction, out hit, 1 << 9);
//return Physics.Raycast(rayCastOrigin, direction, out hit, Vector3.Magnitude(direction), 1 << 9);
}
If you want the closest one, you should use Physics.Raycast, not RaycastAll. But really your code doesn’t return an object at all, just a boolean, so how do you know you’re getting “the wrong one”? Is that still your issue?
Also, please use code tags.
Here the code with code tags:
bool RayCastCheckFieldLayer(Vector3 rayCastOrigin, out RaycastHit hit, Vector3 direction)
{
RaycastHit[] hitCombos = null;
Ray ray = new Ray(rayCastOrigin, direction);
hitCombos = Physics.RaycastAll(rayCastOrigin, direction, 1 << 9); // 1 << 9 is a layer (to select only "them")
// hitCombos = Physics.RaycastAll(rayCastOrigin, direction, Vector3.Magnitude, 1 << 9);
//hitCombos = Physics.RaycastAll(rayCastOrigin, direction);
//hitCombos = Physics.RaycastAll(ray);
// this return - logic is only testwise. later it will return either from the array or from the Raycast(...)
foreach(var r in hitCombos)
{
Debug.Log(r.collider.gameObject.name);
}
/* Debug.Log((hitCombos == null ? "was empty " : hitCombos.Length.ToString()));
if(hitCombos.Length > 0) // testwise
{
hit = hitCombos[hitCombos.Length-1];
return true;
}*/
return Physics.Raycast(rayCastOrigin, direction, out hit, 1 << 9);
//return Physics.Raycast(rayCastOrigin, direction, out hit, Vector3.Magnitude(direction), 1 << 9);
}
the bool is just a check, if a gameobject is found. then this code has in its signature the an out-object “RaycastHit”. With Physics.Raycast to get the closed one is clear, but the ones which are not far away i use the Physics.RaycastAll().
i get a set of gameobjects, but not the one which i need (in the whole list!). If the object would be in that list, i’d add a reckognition logic, but the list is a) empty, when i am using a distance, b) full with objects, but never with the one i need.
to test, i named all the objects unique and evalutated the names in the RaycastAll()-Array - but never found the one i expected.
maybe i should use a “wandering” code, so that i always get the neighbour, cast, and and the other neightbour to get to the goal. but i feel that this way is more expensive then to get a set of gameobjects / a raycastall exact to the point of the gameobject i need (which isn’t working)