Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by EleKid6897 · Jun 29, 2018 at 02:30 PM · raycasthit2dphysics2d.raycast

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; }

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hu90 · Jun 29, 2018 at 02:55 PM 0
Share

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?

avatar image EleKid6897 hu90 · Jun 29, 2018 at 11:54 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
-1

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

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hu90 · Jun 29, 2018 at 03:05 PM 0
Share
 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.

avatar image jamesatighe hu90 · Jun 30, 2018 at 08:08 PM 0
Share

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

avatar image EleKid6897 jamesatighe · Jul 02, 2018 at 07:31 PM 0
Share

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);


Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

87 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Raycast2D not working with LayerMask 1 Answer

Physics2D.Raycast intermittently returning null 1 Answer

How do I get characters to interact with each other? 1 Answer

LinecastNonAlloc, documented syntax gives error... 1 Answer

2D Raycast effect only what it hits? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges