how to detect when a gameobject leaves the screen
void OnBecameInvisible() { Destroy(gameObject); }
how to detect when a gameobject leaves the screen
void OnBecameInvisible() { Destroy(gameObject); }
in unity i want to destroy a gameobject when it hits the edge of the screen
using UnityEngine;
using System.Collections;
public class CarDetection : MonoBehaviour {
private Camera mainCamera;
public Vector2 widthThreshold;
public Vector2 heightThreshold;
void Awake () {
//Get your mainCamera here. If you are pretty sure that the camera is always the Camera.main you don't need to implement here. Just call for Camera.main later.
}
void Update () {
Vector2 screenPosition = mainCamera.WorldToScreenPoint (transform.position);
if (screenPosition.x < widthThresold.x || screenPosition.x > widthThresold.y || screenPosition.y < heightThresold.x || screenPosition.y > heightThresold.y) {
Destroy (gameObject);
}
}
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us