- Home /
I want to delete a sprite when entering a trigger
So I have that 2d game I'm working on and I need to add health to the player when he enters a sprite trigger and I couldn't figure it out.
public float Health;
public GameObject healthTxt;
// Start is called before the first frame update
void Start()
{
Health = 100f;
}
// Update is called once per frame
void Update()
{
if(Health <= 0f)
{
Debug.Log("You Lose :)");
}
}
public void OnCollisionEnter2D(Collision2D collision)
{
if(collision.collider.tag == "Enime")
{
Debug.Log("-50 Health :)");
Health -= 50f;
healthTxt.GetComponent<Text>().text = "Health: %" + Health;
}
}
public void OnTriggerEnter2D(Collider2D collision)
{
if(collision.tag == "+Health")
{
if(Health < 100f)
{
Debug.Log("+10 Health :)");
Health += 10f;
healthTxt.GetComponent<Text>().text = "Health: %" + Health;
Destroy();
}
}
}
}
make sure your player(or sprite?) has also a rigidbody2d attached
the player is a sprite with a rigidbody2d attached to it.
You probably already did these but make sure again to: -object tag is correct -they have collider2d(not 3d) -at least one of them has rigidbody -and make sure health is under 100 when it touches the potion
Answer by N-8-D-e-v · Jun 07, 2020 at 05:20 PM
make sure that the collider or the player have a rigidbody2d attatched, and some form of colliders on both
The Player has rigidbody2d and a collider but the sprite have a collider only.
Try experimenting with debug.log statements to see what sections of your code are getting executed
Your answer
Follow this Question
Related Questions
Collision detection problems with plane + 2D Sprites 0 Answers
double collision detection in breakout game 1 Answer
How to get an AOE attack to deal damage to more than one enemy at a time in 2D? 0 Answers
Trigger collision with multiple objects suddenly becomes unreliable. 1 Answer
problem solving ghost vertices with trigger and collision colliders 0 Answers