- Home /
Need help with creating a 2D polygon from a series of GameObjects in C#
Hi! I'm making a puzzle game about catching rain (circles) from the sky, and clicking the water to turn it into ice (a polygon). The only problem is that I'm a complete noob when it comes to programming with Unity and I only have a very basic understanding of it. I have absolutely no idea how to turn a whole bunch of GameObjects into a solid polygon and need help!! How can I make a script that turns a group of circles into 1 solid object with a click?
Do you want to change shape of those objects? Or you simply want to group them into one entity that looks exactly the same as those objects separated, but moves and rotates as one object?
Yeah the second one, just group them together into an object that can be rotated and has collision!
Answer by rbeldessi · Oct 10, 2018 at 05:35 AM
If you just want to group them together you simply have to generate a new GameObject and then set the parent of each rain drops transform.
GameObject newParent = new GameObject ();
foreach(transform t in raindrops)
t.parent = newParent.transform;
Where raindrops is an array of the transforms of each raindrop. Edit: I misunderstood the question. Completely different answer than the original.
Answer by Casiell · Oct 10, 2018 at 06:52 AM
@rbeldessi answer is actually correct. When you parent all your objects to one game objects, you can then treat that parent as your main object. What you may want to do is, after parenting remove all Rigidbodies from your little objects and add a rigidbody to the parent. Colliders on children objects are ok, they will work normally and they will raise events you can catch in parent scripts.
Answer by theorbzone · Oct 10, 2018 at 07:33 AM
The other biggest challenge I can't wrap my head around is that when you click on 1 drop it should also affect all drops it's connected to. Though thanks a lot for helping to answer!!!
So put a script on the parent object. $$anonymous$$eep a reference of all the drops that it's responsible for and do all the logic from the parent object. The parent should receive all mouse click events that would happen to any of it's child
Thank you, that helps alot! But i still need to figure out how to tell when a raindrop is colliding with other raindrops :(
I'm so close to figuring this out i can taste it!!!!!
If a moment of the collision is enough you can use OnCollisionEnter2D.
If you need to know if two objects are colliding at any given moment you could use OverlapCollider or GetContacts.
I've never really used the last two, so I can't offer much help, but one of those three methods should give you what you want
Your answer
Follow this Question
Related Questions
(noob) 2D sprite jagged edges as smaller? 1 Answer
Generate Polygons and Colliders Runtime in 2D Game 0 Answers
How to keep and grow a polygonal area in 2D 3 Answers
How to render polygons in unity? 0 Answers