How can I make the Player be destroyed when I touch the end of the camera?
hi guys i want my player to be destroyed when i hit the camera ... i used the following scrpits but it did not work
first on the camera I put a collider
and I entered this code
private void OnTriggerEnter2D (Collider2D other)
{
if (other.gameObject.CompareTag ("Player"))
{
Destroy (player);
}
}
but it did not work
so I inserted it into the player
private void OnTriggerEnter2D (Collider2D other)
{
if (other.gameObject.CompareTag ("MainCamera"))
{
Destroy (gameObject);
}
}
and still nothing
please help me!
Answer by CannJRB · Jul 15, 2019 at 11:01 PM
There are a lot of places where this could be going wrong but you can try this...
First, put a Box Collider2D on the Main Camera. Edit that collider so that it is the full size of the viewable area (click the Edit Collider button on the component and drag the green box the the full size). Then check the Is Trigger box.
Be sure your player has a RigidBody 2D and a Box Collider 2D attached.
Put the following script on your Main Camera...
using UnityEngine;
public class DestroyPlayer : MonoBehaviour
{
private void OnTriggerExit2D(Collider2D other)
{
if (other.tag == "Player")
{
Destroy(other.gameObject);
}
}
}
thank man, i am using entercollider2d, with exitcollider2d, is right
Your answer
Follow this Question
Related Questions
How to make flashlight stay with camera? 0 Answers
Set Main Camera in Networkmanager 1 Answer
Gameobject doesnt move after build 0 Answers
How to prevent duplicated gameObjects with disabled camera? 0 Answers
Camera Editor,Camera Editor 1 Answer