Reputation: 661
I want to have certain objects only collide with certain tilemaps in unity. For example, I have a player and I want to be able to change which tilemaps his collider interacts with. When he is on the ground I want him to interact with the water and not be able to go into the water, but when I am in a boat I would like to not collide with the water tilemap but instead collide with the ground tilemap. Is there a way to do this?
Upvotes: 2
Views: 990
Reputation: 95
You can make the water and the ground separate tilemaps, and enable their tilemap collider components via script. Follow these steps to include this functionality in your existing script that handles the player entering the boat:
First, include the Unity Tilemaps at the top:
using UnityEngine.Tilemaps;
Then declare the tilemap collider variables:
[SerializeField] TilemapCollider2D groundCollider = null;
[SerializeField] TilemapCollider2D waterCollider = null;
Inside your method that gets called when the player enters the boat:
groundCollider.enabled = false;
waterCollider.enabled = true;
Then do the opposite whenever the player exits the boat:
groundCollider.enabled = true;
waterCollider.enabled = false;
Just don't forget to drag the tilemap collider components into your script variables in the editor, and you should be good to go.
Upvotes: 1