Using a Tilemap composite collider as a trigger, what is the best approach for getting hold of the subcollider that is actually being triggered?
Using a Tilemap composite collider as a trigger, what is the best approach for getting hold of the subcollider that is actually being triggered?
This seems a lot less straight forward than perhaps it should be. I'm using a Tilemap with a composite collider set up as a trigger to draw ladders on the map, and want to lock the player to the central x point of the ladder when they're climbing it. However, I'm hitting a wall in that it seems very difficult to simply get ahold of the individual tile that is being collided with.
The method mentioned elsewhere only works for collisions (ie non-triggers), and even that seems somewhat convoluted for what I would've expected to be a routine task.
Does anyone have any suggestions how to do this? I need to either somehow get the contact point of the trigger (seems to be unsupported) in order to then fire a ray to get the collider, or otherwise add additional gameobjects to the scene that can act as anchors for the ladder, which seems to go against the point of using tilemaps.
Thanks for any help.
1 Answer
1
There is a way to create a separate collider at the tile location of the tile map to use that location.
It is more accurate than raycast.
Because it is an example script, please refactor and use it
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
[RequireComponent( typeof( Tilemap ) )]
public class TilemapCollider : MonoBehaviour
Tilemap t;
void Start()
t = GetComponent<Tilemap>();
BoundsInt bounds = t.cellBounds;
TileBase allTiles = t.GetTilesBlock( bounds );
var s = t.layoutGrid.cellSize / 2;
var availablePlaces = new List<Vector3>();
for( int n = t.cellBounds.xMin; n < t.cellBounds.xMax; n++ )
for( int p = t.cellBounds.yMin; p < t.cellBounds.yMax; p++ )
Vector3Int localPlace = ( new Vector3Int( n, p, (int)t.transform.position.y ) );
Vector3 place = t.CellToWorld( localPlace );
var tile = t.GetTile( localPlace );
if( tile )
availablePlaces.Add( place );
var c = new GameObject().AddComponent<BoxCollider2D>();
c.isTrigger = true;
c.transform.parent = t.transform;
c.transform.localPosition = t.CellToLocal( localPlace ) + s;
Debug.Log( "x:" + n + " y:" + p + " tile:" + tile.name );
TilemapCollider.cs
private void OnTriggerEnter2D( Collider2D collision )
var tilemap = GameObject.Find( "Grid/Tilemap" ).GetComponent<Tilemap>();
tilemap.SetTile( tilemap.WorldToCell( collision.transform.position ), null );
Example of finding a location on OnTriggerEnter2D
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.