Question by
Steven4547466 · Feb 08, 2021 at 04:33 PM ·
c#raycastcollider2dtilemapcollision2d
Bounds and extents of a tilemap collider across differing y axes, Tilemap collider bounds
Hello, I'm trying to position a sprite directly on top of a ground layer, and it was working fine. However, I added new tiles that aren't at the same y level and the bounds of the collider seem to always be the highest one regardless of the collider that I've hit. I tried using a tilemap collider with a composite collider and just the tilemap collider alone, both didn't work so I'm kind of stuck.
This is just prototype code, so it's not meant to be efficient yet
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 pos = new Vector2(mousePos.x, mousePos.y);
RaycastHit2D hit = Physics2D.Raycast(pos, Vector2.down, 1000f, LayerMask.GetMask("Ground"));
if (hit.collider != null)
{
pos.y = hit.collider.bounds.max.y + collider.bounds.extents.y;
}
transform.position = new Vector3(pos.x, pos.y, 0);
This always results in the sprite being on the top y axis instead of floor level.
Here's an image without the extra tiles:
Like I said I tried it with and without a composite collider. Is the only way to fix this to add a new tilemap with a separate collider?
Thanks
Comment