Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Unity Raycast2D results are not as expected?

Discussion in 'Scripting' started by Jovy83, Dec 10, 2015.

  1. Jovy83

    Jovy83

    Joined:
    Feb 28, 2015
    Posts:
    15
    I'm currently working on a practice project which involves stacking of blocks. It is sort of similar to the game of stackers (the arcade game), except my version uses free-form movement instead of grid-based movement.

    I have 3 prefabs for the blocks: SingleBlock, DoubleBlock, TripleBlock.

    I structured each of them like this: the parent is an empty gameobject with my MovementScriptthat moves them left/right, while the children are the block sprite with a BoxCollider2D.

    The MovementScript is attached to the empty game object(the parent) so that the block set moves uniformly left/right, for what it's worth.

    For the actual stacking logic, I'm using Raycast2D to detect if there is a block below. But the problem is the results I get is unexpected.

    Here is a snippet of my current code:
    Code (CSharp):
    1. foreach(Transform t in currentBlockSet.transform)
    2. {
    3. // get all the children of this blockset. to do this, we use the transform beause it is IEnumerable
    4. GameObject block = t.gameObject;
    5. RaycastHit2D hit = Physics2D.Raycast(block.transform.position, Vector3.down, rayCastLength); // 0.5f raycast length
    6. //Debug.DrawRay(block.transform.position, Vector3.down * rayCastLength);
    7. if(hit.collider != null)
    8. {
    9. // this means there is a block below, we hit something
    10. Debug.Log("True");
    11.  
    12.  
    13. }
    14. else
    15. {
    16. Debug.Log("False");
    17. }
    18.  
    19. }
    This code is called each time the player stops the current blockset that is moving by the way.

    The problem is I always get true in my logs even though I purposely didn't align the block set properly. I never get false even if I'm way off with my alignment. Why is this so?

    I do like to mention that there's nothing else in the scene. It is just the blocks, so there can't be another object to collide with.

    Is there something wrong with the logic or how I'm using Raycast2D?

    Appreciate any help.
     

    Attached Files:

  2. troyfury

    troyfury

    Joined:
    May 15, 2015
    Posts:
    19
    It could be colliding with another collider, even its own. Try assigning each block the tag "Block" and then checking in code if the raycast hit is hitting an object tagged Block.

    1. foreach(Transform t in currentBlockSet.transform)
    2. {
    3. // get all the children of this blockset. to do this, we use the transform beause it is IEnumerable
    4. GameObject block = t.gameObject;
    5. RaycastHit2D hit = Physics2D.Raycast(block.transform.position, Vector3.down, rayCastLength); // 0.5f raycast length
    6. //Debug.DrawRay(block.transform.position, Vector3.down * rayCastLength);
    7. if(hit.collider != null)
    8. {
    9. if(hit.collider.gameObject!=gameObject && hit.collider.tag=="Block"){
    10. Debug.Log("True");
    11. }

    12. }
    13. else
    14. {
    15. Debug.Log("False");
    16. }

    17. }
     
  3. Jovy83

    Jovy83

    Joined:
    Feb 28, 2015
    Posts:
    15
    Thanks for the reply. So I have tried debugging with the collider.tag and collider.gameObject.name as you suggested and I can confirm that the block/raycast is colliding with its own collider. Hence, I always get true in my log.

    But how do I make sure that it doesn't collide with itself and actually collide with the block set below? Do you know a viable solution for this? One way I can think of is to create an empty object just below the block's collider and make that the origin of the raycast. Is there a better way by any chance?
     
  4. troyfury

    troyfury

    Joined:
    May 15, 2015
    Posts:
    19
    There are multiple solutions, the one you suggested can work, you can check that the raycast hit isn't your self :
    if(hit.collider.gameObject!=gameObject) or you can use layermasks to make the raycast ignore certain objects, although this might not be suitable in this case as the object firing the ray would have the same Layer as the objects it is trying to detect. I think making sure the ray origin is outside of its own collider would be the best way for this case. This is a solution that is often provided in the Unity sample projects.
     
  5. Jovy83

    Jovy83

    Joined:
    Feb 28, 2015
    Posts:
    15
    Thanks, I got the idea and was able to make the raycasting work properly in my case!