- Home /
Find the highest point of a stack of gameobjects
I am writing a 2D game. I have a jumbled pile of gameObjects and I want to work out the highest point of the highest of those objects. I was going to use a kinematic trigger object and move it downwards until it hit something, but moving it by code doesn't cause any OnTriggerEnter2D or OnCollisionEnter2D calls. I'm sure there must be a more sensible way to measure it than by dropping a collider onto the pile - can someone point me in the right direction?
Answer by HarshadK · Jun 11, 2014 at 12:38 PM
You can store the position transform of all your game objects in an array or list and then find the game object with highest 'y' value for the position (assuming you are using 'y' value to check the highest position) using any basic sort algorithm like bubble sort.
That would find the highest centre of an object, but I want to find the highest point of the highest object. I guess I could use the object's bounds, but then an object with a lower y might be larger and have a higher high point.
Then you can add up the values of bounds of all the game objects that are in the stack to get the highest point.
Yes I reckon that's going to be the fastest and simplest solution. Thanks!
Answer by FabDynamic · Apr 12, 2018 at 02:20 AM
This is similar to the answer by @HarshadK
Step 1: Figure out how to obtain the HighestPoint for each object Step 2: Use Step 1 to obtain a list of all HighestPoints Step 3: Sort the list
I'm going to presume that you can do Steps 2 and 3 but step 1 is tricky. To do this use the renderer bounds which are in world coordinates (not the mesh bounds). To get these values you will need to use:
<Insert gameobject here>.GetComponent<Renderer>().bounds.max.y
I hope that helps.
Your answer
Follow this Question
Related Questions
[2020.3][2D Collision] Trigger collider with physics computation 0 Answers
OnTriggerEnter2D problem on created collider2d and rigidbody2d during runtime 0 Answers
Trigerrs Logic problem with collision damage 1 Answer
OnTriggerExit2D not working, but OnTriggerEnter2D does??? 5 Answers
How to use Physics2D.IgnoreCollision2D in Unity 5 to cancel a trigger 0 Answers