- Home /
How to achieve this basic physics effect? Detect when to change layers
In the game Bomberman, when you first place down a bomb it is placed directly on top of you, and you can you move left, right, up, or down to move away from it. Once you've moved away from it you can no longer walk through it - it becomes like a wall.
I'm trying to achieve something similar in Unity - when you first place an object it's right where you are, and it's translucent (not visually, but just in terms of being able to move through it). However as soon as you leave its boundary, it becomes solid and cannot be moved through.
The way I thought to do this initially was to set a physics layer on the bomb that does not interact with the player - and then as soon as the player leaves the boundary, change it to another layer. The problem with that is how to detect when the player has left the bomb if the two are not colliding.
One potential solution is to use Update() to check the distance between the bomb and the player manually every step, but that seems cumbersome and error prone.
Any ideas?
Answer by GrayRabbitGames · Nov 21, 2019 at 07:33 PM
You know what I actually just went ahead and did what I said I thought was cumbersome and it's not as bad as I thought. I only check when the character moves (instead of Update()) and only distance between character and last object they placed. So I'm fine with that solution.
Answer by lgarczyn · Nov 21, 2019 at 11:21 PM
You can just use IgnoreCollision with the player on the creation of the bomb, and then again with a false
argument when the player gets far enough away. Instead of using distance, you can use collider.bounds.Intersect
.
Your answer
Follow this Question
Related Questions
Physics Collision Layer Programmatically. 0 Answers
LayerMask is incorrect 1 Answer
Button Box Door Collision 2 Answers
How to collide with everything but trigger script whenever certain object hits 1 Answer
using IsTouchingLayer without collision 0 Answers