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 darthtelle · Aug 05, 2014 at 11:16 AM · gameobjectphysicsraycastrotateraycastall

RaycastAll isn't finding all possible collisions

In my game you have the ability to flip board tiles, and I'm using a "fake" grid system to hold tile information. What I'm trying to do is detect that when a tile has been flipped what is now on the other side.

To do this I have a game object which represents the tile and holds 18 more game objects which represent the squares; 9 facing up and 9 facing down. The tile parent object is flipped using Transform.Rotate depending on whether it is flipped left to right, or top to bottom, and vice-versa. Once the "fake" grid has been flipped I raycast from each of the 9 grid positions.

Usually, this system works brilliant! The raycast finds all the hits and filters out the correct game object it is seeking. Unfortunately, occasionally, but frequently enough to ruin my game logic, it doesn't find the raycast hits from the flipped tile. I know the raycast is working, however it doesn't return the tile I'm looking for.

This code is called many times inside a deep-rooted coroutine because it's calculating some AI calculations. I have tried the route of waiting a frame after flipping the tile to raycast, I have also tried various colliders on the objects (currently they are box colliders which aren't triggers and do not have a rigidbody). However, I still have the same issue.. sometimes it works, sometimes it doesn't.

Is there a particular reason that Physics.RaycastAll does not return ALL of the collisions? Could flipping the game object sometimes prevent this from being detected?

I have included the flipping and raycasting code below, but have omitted my Debug checks. They just confirm when it isn't working, and what the raycast is finding.

 // Flip the tile object
 switch(flipDirection)
 {
     case TileController.Flip_Direction.Left:
     case TileController.Flip_Direction.Right:
         tileParent_Object.transform.Rotate(new Vector3(0.0f, 0.0f, 180.0f));
      break;
             
     case TileController.Flip_Direction.Up:
     case TileController.Flip_Direction.Down:
         tileParent_Object.transform.Rotate(new Vector3(180.0f, 0.0f, 0.0f));
     break;
 }
 
 float yPosition = -2.5f;
 float distance = 5.0f;
 
 int layerMask = 1 << LayerMask.NameToLayer("Invisible");
 
 // Raycast from the grid object to determine what is hit
 for(int squareIndex = 0; squareIndex < tileGrid_Object.m_Squares.Count; squareIndex++)
 {
     // Find the position of the grid on the original board and adjust to accomadate ray
     Vector3 grid_Square_Position = tileGrid_Object.m_Squares[squareIndex].transform.position;
     grid_Square_Position.y = yPosition;
 
     Ray square_Ray = new Ray(grid_Square_Position, Vector3.up);                    
     RaycastHit[] hit_List = Physics.RaycastAll(square_Ray, distance * distance, layerMask);
             
     //Debug.DrawRay(grid_Square_Position, Vector3.up, Color.white);
             
     for(int hitIndex = 0; hitIndex < hit_List.Length; hitIndex++)
     {
         if(hit_List[hitIndex].collider.gameObject.tag == Tags.Clone_Square)
         {
             tileGrid_Object.m_Squares[squareIndex].GetComponent<Square>().m_Type = hit_List[hitIndex].collider.gameObject.GetComponent<Square>().m_Type;
             tileGrid_Object.m_Squares[squareIndex].GetComponent<Square>().m_Exit = hit_List[hitIndex].collider.gameObject.GetComponent<Square>().m_Exit;
 
             break;
         }
     }
 }
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 robertbu · Aug 05, 2014 at 02:29 PM 0
Share

You are using a two-sided collider like a box collider for your tiles? Have you done a Debug.DrawRay() of the right length and exa$$anonymous$$ed the geometry in the Scene view during a failure?

avatar image darthtelle · Aug 05, 2014 at 02:35 PM 0
Share

Yes, I have. The ray starts below the box collider. I have tested different distances as well, including the ray finishing inside and outside the collider.

If I don't rotate the tile object the raycast has no problem detecting the box colliders, however, after colliding it is less likely to detect the collision.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Wellmann · Aug 18, 2016 at 09:37 AM

This question is quiet old but if someone has a similar problem in the future: I've had a similar problem. What I found out is that the Rect Transform of the object has to be bigger than the Collider. Other wise the Collider will be trimmed by the Rect Transform bounds.

Comment
Add comment · 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

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

23 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

Related Questions

Detecting other Objects when placing Object 1 Answer

Drag Rigid Body No Distance Limit and how to add Mouselock to a scrip 1 Answer

Melee-like function? 1 Answer

Problem with the direction of the raycast when rotating 1 Answer

Rotate object without rotating RayCast 4 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