- Home /
Raycast2D Help
I'm trying to raycast out of a gameobject to see if there is a specifically tagged object above it, but I don't really know what I'm doing wrong. I followed Unity's raycasting tutorial pretty closely, but it just doesn't want to work. Can someone help me? Here's the code I'm using; the big issue seems to be with my Raycasthit2D variable.
RaycastHit2D HitUp; if(Physics2D.Raycast(transform.position, Vector2.up, out HitUp, RaycastDistance, Mask.value)) if(HitUp.collider.gameObject.tag == "Empty Space"){ Selectable = true; } else{ Selectable = false; }
What happens?
Does the raycast hit the wrong object, or simply nothing?
What's $$anonymous$$ask.value?
Is your tag referenced in the Tags and Layers $$anonymous$$anager?
When I try to run the scene, I just get an overload method error saying that I have some invalid arguments; specifically highlighting the "out HitUp". As for the mask.value, that's a layermask variable.
Answer by jamesatighe · Jun 29, 2018 at 02:56 PM
Looking at the code I don't see how that would work?
You declare the new RaycastHit2D object then run an if statement with the results going into the HitUp variable.
You need to have the rest of your script in the script body of the if loop.
RaycastHit2D HitUp;
if(Physics2D.Raycast(transform.position, Vector2.up, out HitUp, RaycastDistance, Mask.value))
{
if(HitUp.collider.gameObject.tag == "Empty Space")
{
Selectable = true;
}
else
{
Selectable = false;
}
}
This will mean if the Raycast comes back with any result. It will then run the if loop and check the collider.gameObject.tag.
Or instead declare the RaycastHit2D object with the Raycast
RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.up, out HitUp, RaycastDistance, Mask.value)
Then perform the if statement to check the colliders.
This should work.
James
if (...)
if (...) {}
else {}
although questionable in style, is equivalent to:
if (...)
{
if (...) {}
else {}
}
Your suggestion is better, but it doesn't solve OP's problem.
After re-reading yes you are correct.
However upon a further read is it obvious that the OP is not using the right parameters for the Raycast method.
Physics2D.Raycast does not except the parameters the OP is calling.
Look at the Physics2D.Raycast method as shown in the Unity docs.
https://docs.unity3d.com/ScriptReference/Physics2D.Raycast.html
The command should either be:
public static RaycastHit2D Raycast(Vector2 origin, Vector2 direction, float distance = $$anonymous$$athf.Infinity, int layer$$anonymous$$ask = DefaultRaycastLayers, float $$anonymous$$Depth = -$$anonymous$$athf.Infinity, float maxDepth = $$anonymous$$athf.Infinity);
or
public static int Raycast(Vector2 origin, Vector2 direction, ContactFilter2D contactFilter, RaycastHit2D[] results, float distance = $$anonymous$$athf.Infinity);
In the OP's case he is passing the layermask in the wrong position if he is attempting to return the Raycast out to a RaycastHit2D.
It looks like he is using the context for the Physics.Raycast NOT Physics2D.
public static bool Raycast(Vector3 origin, Vector3 direction, out RaycastHit hitInfo, float maxDistance = $$anonymous$$athf.Infinity, int layer$$anonymous$$ask = DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);
If the OP changed to use the Physics.Raycast the parameters he has would be correct. But if they are using Physics2D I would use a ContactFilter2D object set to use the Layer$$anonymous$$ask as below
Protected ContactFilter2D contactFilter;
void Start ()
{
contactFilter.useTriggers = false;
contactFilter.SetLayer$$anonymous$$ask(Layer$$anonymous$$ask.Get$$anonymous$$ask("Ground"));
contactFilter.useLayer$$anonymous$$ask = true;
}
Then in the FixedUpdate you could put:
RaycastHit2D hitUp = Physics2D.Raycast(transform.position, Vector2.Up, contactFilter, hit, RaycastDistance);
if (hitUp.collider.gameObject.tag == "Empty Space")
{
Selectable = true;
}
else
{
Selectable = false;
}
This should then allow the code to work.
I have tested this and it works fine.
James
Thanks for the suggestion. $$anonymous$$y only question is what is the "hit" variable you are using in the fixed update? I don't think it's from my code, and I've been looking at the scripting api, but I'm still having trouble understanding what it is that you are using. I'm pretty new to raycasting, so sorry if this seems like a dumb question.
RaycastHit2D hitUp = Physics2D.Raycast(transform.position, Vector2.Up, contactFilter, **hit**, RaycastDistance);