- Home /
Unity Raycast2D results are not as expected?
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 MovementScript that 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:
foreach(Transform t in currentBlockSet.transform)
{
// get all the children of this blockset. to do this, we use the transform beause it is IEnumerable
GameObject block = t.gameObject;
RaycastHit2D hit = Physics2D.Raycast(block.transform.position, Vector3.down, rayCastLength); // 0.5f raycast length
//Debug.DrawRay(block.transform.position, Vector3.down * rayCastLength);
if(hit.collider != null)
{
// this means there is a block below, we hit something
Debug.Log("True");
}
else
{
Debug.Log("False");
}
}
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.
Answer by GiyomuGames · Dec 10, 2015 at 07:26 AM
Your raycast is hitting the collider of the blockset from which you are casting no? Anyway you should definitely check which object is hit to understand more. You can also draw the ray as a gizmo to see what's happening in the editor.
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?
You can "manually" set the cast position outside of the collider when you call Physics2D.Raycast. You can calculate it using the collider position and bounds I believe. It's probably something like collider.transform.position - Vector3.up * collider.bounds.y / 2. Not sure, need to test it ^^
Thanks I got the idea already and got it working the way it's supposed to.