- Home /
How to make the game win once the correct height is reached
Hello, gonna try to keep this short and concise like the rule says. Sorry if this is weird or bad or something. I'm trying to make a game thing kinda like those stacking games or tetris but 3D. You control an object in the air, and when you press a certain button, it drops. If it lands on a target platform thing, you get a point and it spawns a new object, and you basically repeat that process until the blocks stack to reach a certain height, and you win the level. If it misses that target, then game over and reload scene.
Anyways, problem with that last part. How do I make it so when they stack on each other and reach that height, it activates a UI or something like that, and when you release the object it doesn't accidentally activate it when it goes through it? Once again, sorry if this is a really dumb or easy question, but I am just a newbie. Thanks in advance!
Answer by Riki9811 · Aug 17, 2020 at 02:07 PM
I think there are 2 ways of doing it.
First way:
Whenever an object falls and hits your platform use:
this.GetComponent<Renderer>().bounds.max.y
This will return the y of highest point of the object, and you can check if it is above the value you want
Second method:
Set up a trigger collider at the hight that you want
Give this new gameObject a tag, let's say "Finish line".
Then in the script to the objects you spawn in write:
void OnTriggerEnter(Collider other) {
if (other.gameObject.CompareTag("Finish line"))
{
//Do something
}
}
Then set the "Finish line" to be disabled by default.
Every time the falling object lands enable the "Finish line" gameObject. Doing so will also trigger every OnTriggerEnter() event on all the objects that go through it.
If you haven't reached the height you wanted then simply disable the "Finish line" again before spawning the new object.
I think the firs method is cleaner and mush simpler, but I'm not sure if it can give you some problems with reading the highest point of some 'oddly' shaped 3D models.
I tried using your first method and wooo it works ($$anonymous$$aybe because the shape of my object is slightly strange so it's sometimes a bit off but it works 99% fine normally). Thanks for helping!
Answer by JackhammerGaming · Aug 17, 2020 at 02:10 PM
@Amberlewis012 i am gonna try my best from what i understood from what you need try something like
GameObject last_instantiated_object;
public float win_pos; // set this equal to the y co-ordinate of the height after which level is won
public void OnButtonClick(){
last_instantiated_object = Instantiate(stack ,transform.position); // stack is the object which is getting dropped i don't know the name of your variables in your script
if (last_instantiated_object.transform.position.y >= win_pos){
ui.SetActive(true); // ui is a variable name for win screen gameobject
}
}
last_instantiated_object is the variable name for the object you last instantiated or simply the object which is being dropped. it will be better if you share your script too
I did what the other guy wrote already and it works so this isn't a problem anymore. Thanks for helping though, seriously!