- Home /
Check GameObject postion
Hello everyone, I'm developing my first 2D game. It's obviously very simple but since I'm new there i found some problems. The game purpose is to dodge obstacles with your gameobject which can move left and right, i'm wondering how can I make the update method check if one obstacle occupy a position, for example when the rocket (obstacle) position is on (0, -4, 0), destroy the object and add +1 to the score. I didn't found anything on internet so that's why i'm asking that to you! hope you can help me, thank you in advance!
Answer by Jessespike · Nov 13, 2015 at 07:16 PM
You can place Colliders in the scene that will call a function whenever a collision occurs. Unity Manual: Colliders
void OnTriggerEnter(Collider other) {
Destroy(other.gameObject);
UpdateScore();
}
You could also calculate the distance of the object and the position to determine a collision:
void Update()
{
Vector3 collisionPosition = new Vector(0f, -4f, 0f);
if (Vector3.Distance(this.transform.position, collisionPosition) < 1f)
{
UpdateScore();
Destroy(gameObject);
}
}
This code isn't meant to work from a straight copy-paste, it's to give you ideas.
Thanks Dude! this was'nt the problem i was looking for but what you've suggested has worked for me like a charm! So Basically i was trying to make my camera start moving with my player after my player has reached at a specific point. i know it sounds dumb but i've used this onTrigger Events many times before but your trick helped me to go in the right path! NOW I A$$anonymous$$ HAPPY WITH $$anonymous$$Y PROJECT! A Ton Thanks :)