Merge Objects In 2D Unity Game | Create A New Object Dragging One Object to Another Combining Them

Опубликовано: 04 Октябрь 2024
на канале: Alexander Zotov
25,832
528

How to merge two objects to get a new one? That is what this video is about.

TO BLAST! - My New Fun Relaxing Puzzle Game Available On Google Play Store
https://play.google.com/store/apps/de...

Not only your hands are responsible for aiming ability :-)
Logitech G305 LIGHTSPEED Wireless Gaming Mouse
https://amzn.to/2VxEOrM

Consider purchasing my ULTIMATE UDEMY COURSE with a great discount https://www.udemy.com/how-to-make-gam...

If you like what I'm doing then you can support me through
www.patreon.com/alexanderzotov
or here
https://www.paypal.me/AZotov
Thank you)

Here what this video is about.
How to merge objects to create a new one? Let me show a very simple way.
Here I have an empty project. Let’s add some gameobjects. Those will be an acorn, an oak, and a Rocket of course. These gameobject gonna need box colliders to detect mouse click. And also to detect collisions between each other they will need a rigidbody 2d. Set gravity scale to 0 or they will fall down. Ok. One simple script helps us to make this all work.

OnMouseDown is invoked when you press left mouse button over objects collider. That’s why we need those colliders. Without collider it will not work. So when it happens I kind of declare that I hold mouse button down ‘cause I'm gonna need some checking when I release it. Also here I calculate some offset between mouse position and center of game object I’m going to drag. Without this offset my gameobject will jump upto mouse pointer when I start to drag it.

OnmouseDrag is invoked when I drag an object with mouse holding down left mouse button. Everything is pretty simple here. I get Mouse position and set objects position to it using that offset.

OnMouseUp is invoked when mousebutton is released. When it happens I tell the script that button is released and this condition is met. The main part of the magic happens in ontriggerstay method which is being invoked while gameobjects collider is overlapping another collider. So when it’s happening I have to get names of those overlapping gameobjects to be able to perform merge action. Or not to perform. So I get names here. Underscore divider which I add to objects names a bit later will help me to get the part of the name I need. First part in this case which will be Acorn, Oak or Rocket.
Ok. When names are obtained I check if a button is released and names are equal to what I want them to, I instantiate a new Oak or rocket depending on certain conditions. Prefabs will be loaded from the resources folder which I will create in a moment. That’s the script.

I D&D this script to Acorn, Oak and Rocket. To be able to instantiate gameobject I need a prefab of it. And this prefab should be put into resources folder. So I create resources folder in assets folder. Be careful with resources folders name. Just one typo will ruin the whole game. Once the folder is created I D&D my gameobject into it turning them into prefabs. Now I have to rename each prefab so the script could get the part of its name and load its prefab correctly. Adding underscore and object word will do the job. Nice. I can delete these gameobjects from hierarchy. Now I add an Acorn. duplicate it three times. Spread them around the scene a bit. And now I can hit play to see how it works. I can drag each object. And when I overlap them and release a b…. wtf… Ah. I forgot to set colliders to be triggers. Select my prefabs and check in isTrigger option. Now I can hit play and see how it works. I can drag it and if I overlap them and release a button a new gameobject is created. Beautiful. Hope you like this tutorial. Thank you for watching and see you next time.