Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 arnaldo42 · Jan 26, 2014 at 02:40 AM · raycastlinecast

Using a raycast to check for objects between two sprites?

Hi, all!

I'm using the 2D toolset from 4.3 to make a game where you grapple onto specific objects. I want to use a variable function to get a raycast/linecast from the player and the object to grapple onto it. I call this variable function OnMouseOver as a condition to check if there are objects in-between both sprites. I have tried using Physics2D but for some reason it is not recognizing the objects. The bool will always return false as if there is something in the way when there isn't. I've tried both Physics2D and regular Physics Raycasts and Linecasts with no results. The player has a RigidBody2D and a CircleCollider2D while the node to attach to has a Circle Collider 2D.

Any ideas?

 bool GrappleInRange(Transform origin, Transform end, float distance)
     {
         Ray ray = new Ray();
         ray.origin = origin.position;
         ray.direction = end.position - origin.position;
         bool hitGoal = false;
         RaycastHit2D col = Physics2D.GetRayIntersection(ray, distance);
 
         if(col)
         {
             print(col.collider.name);
         }
 
         if (col.transform != end)
         {
             hitGoal = false;
         }
         else if (col.transform == end)
         {
             hitGoal = true;
         }
 
         return hitGoal;
     }


 void OnMouseOver()
     {
         if(GrappleInRange(player.transform, transform, connectionDistance) == true && connected == false)
         {
             connectIcon.SetActive(true);
         }
 }



-Arnaldo

Comment
Add comment · Show 4
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 Benproductions1 · Jan 26, 2014 at 02:47 AM 0
Share

Try debugging the ray by using Debug.Ray

avatar image arnaldo42 · Jan 26, 2014 at 03:06 AM 0
Share

Tried using debug.drawray but it doesn't show the full ray. It's just a tiny ray co$$anonymous$$g from the player. $$anonymous$$aybe it's not reaching the other sprite?

 Debug.DrawRay(ray.origin, ray.direction, Color.red);
avatar image Invertex · Jan 26, 2014 at 03:08 AM 0
Share

To extend the ray distance, just multiply ray.direction.

 Debug.DrawRay(ray.origin, ray.direction * 5.5f, Color.red);

for example.

avatar image arnaldo42 · Jan 26, 2014 at 03:18 AM 0
Share

That did the trick.

Yeah, the Debug.DrawRay reaches the sprite On$$anonymous$$ouseOver but it is still giving me the same result even if there isn't an object in the way.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Invertex · Jan 26, 2014 at 03:19 AM

I would probably do it using Linecast, you also need to set some LayerMask's, so it doesn't get triggered by any type of collider, such as the player:

     private RaycastHit2D[] hits = new RaycastHit2D[2];
     private Vector2 grapplePos;
     private int openSpace;
     
     void GrappleCheck()
     {
         //grapple position stuff
         grapplePos = whatever;
         //check for objects between grapple position and this player
         openSpace = Physics2D.LinecastNonAlloc(transform.position, grapplePos,hits,(1 << LayerMask.NameToLayer("Ceiling")) | (1 << LayerMask.NameToLayer("Wall")) | (1 << LayerMask.NameToLayer("Ground")));
 }

    void OnMouseOver(){
     
         //if only 1 collider in any of those Layer's was found, then you can grapple. If more than 1 or 0 were found, then don't grapple.
         if (openSpace == 1)
         {
             //do the grapple
         }
         
     }
Comment
Add comment · Show 2 · 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 arnaldo42 · Jan 26, 2014 at 11:31 PM 1
Share

That did the trick! Didn't know about Layer$$anonymous$$asks so I had to do some research but it worked beautifully. Used your example to apply it to the variable function and used the output as the condition on the On$$anonymous$$ouseOver/Down methods.

Thank you for your help!

-Arnaldo

 bool GrappleInRange()
     {
         bool inRange;
         Layer$$anonymous$$ask everythignButPlayerBG = ~((1 << Layer$$anonymous$$ask.NameToLayer("Player")) | (1 << Layer$$anonymous$$ask.NameToLayer("Background")));
 
         openSpace = Physics2D.LinecastNonAlloc(transform.position, player.transform.position, hits, everythignButPlayerBG);
 
         if(openSpace == 1)
         {
             inRange = true;
         }
         else
         {
             inRange = false;
         }
 
         return inRange;
     }
avatar image Invertex · Jan 26, 2014 at 11:38 PM 0
Share

Great to hear :) $$anonymous$$ake sure to accept the answer! Thanks

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

20 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Problems using an or (II) inside of an if statement. 1 Answer

racing game raycast problem 2 Answers

How do I change the value of a tag with raycasting? 0 Answers

Issues with Linecast/Raycast 1 Answer


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