Pardon me if I ask stupid question, I am new to Unity3D. Thanks.
I am trying to create a 2D game with the an Orthographic camera, assuming my game area are the whole visible horizontal area.
My GameObject will come from left to right, meaning spawning from left, and out of play when it moves beyond the right border of the game area.
What I am doing now, I hardcoded the right, meaning if the GameObject position.x is more than +8f (assuming this is my screen left border), I destroy them.
But I believe this is not an efficient way, I would like to know whether there is a way to do this detection, rather than hardcode something like this:
if (gameObject.transform.position.x > 8f)
Destroy(gameObject);
Hope someone can help me on this.
Thanks, and wish you to have a nice day.
what you are doing now is efficient enough way. What do you want to do that makes you want to use a different method?
The less expensive method is to use a trigger collider in the position you want to remove GameObjects.
UnityScript:
function OnTriggerEnter (other : Collider) {
Destroy(other.gameObject)
}
C#:
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
void OnTriggerEnter(Collider other) {
Destroy(other.gameObject);
}
}
The collider event will be triggered when 2 colliders (with 1 of them is rigidbody) collide, but for my case, I want to detect when the GameObject move out from the screen area, which I don’t have anything at the screen border, the event didn’t trigger in this case…
what you are doing now is efficient enough way. What do you want to do that makes you want to use a different method?
– GradesFisher