- Home /
How do i make a red lava square do damage when you touch it. (2D)
I need help. I don't know how to script. And i have a red square under my map so whenever a player falls they touch the lava and die. Also the game is 2D. I don't know how to script or code. Please help i need to make this game better. I tried searching: Unity how to change a value when you do a collision. I couldn't find anything.
Answer by Llama_w_2Ls · May 20, 2021 at 05:30 PM
Any simple Unity tutorial would have covered colliders, for sure. They are very easy to use, and not hard to code at all. Here's how you would do it:
Add a collider component to your lava square, that covers the entire area. It could be a box collider 2d or a polygon collider (if the sprite is more complicated). Then, set the collider to Is Trigger. This will allow the player to fall through the collider, whilst still detecting a collision.
Finally, add a script to the lava object that looks something like this:
public class Lava : MonoBehaviour
{
// Called whenever an object passes through the collider
void OnTriggerEnter2D(Collision2D collider)
{
// Destroys the player object if it enters the collider
Destroy(collider.gameObject);
}
}
If you have any questions, please see the docs or see a simple tutorial, for example this one: Collisions in Unity
Your answer
Follow this Question
Related Questions
Photon Combat script does not work 0 Answers
How does the player take damage if collided with enemy? 1 Answer
Health below zero but shows negative numbers 1 Answer
Object Not Recieving Damage 3 Answers
Expecting ) found = 1 Answer