The question is answered, right answer was accepted
Merging two Gameobjets into one!
Hey guy's, I'm trying to figure out a way to merge two game objects the way I want them to, but I have no idea how to even begin... Here's the deal:
I've got a 2D platformer where you can shoot projectiles called blobs. Those blobs are just cubes with dimestions of 0.1, 0.1, 0.1 for now. I'd like them to merge into a bigger "blob" when they collide (or come close to each other) and this bigger blob should have the same properties so that when a smaller blob hits it, they merge and the blob gets even bigger. So that 2 blobs merge into one blob with dimensions of 0.2, 0.2, 0.2 and when another small blob hits it, they merge into a blob with dimensions of 0.3, 0.3, 0.3 and so on.
I hope I wrote that in a way that it is understandable, because like I said, I have no idea how to even begin... To make things a bit more simple, you can just give me some tips to how I can do that, instead of writing the whole code for me, since I'm in the process of learning and wouldn't mind if I had to figure out the specifics myself (that doesn't mean I'd dismiss written code ^^).
When you need more information's about the blobs (or anything really), I will of course try to provide them as fast as I can!
Thank you in advance, and have a nice day!
Answer by TBruce · Apr 18, 2016 at 12:47 AM
If I understand you correctly you want to scale a current GameObject by another GameObject. Try the following (you can also modify any other properties here)
using UnityEngine;
using System.Collections;
public class BlobManager : MonoBehaviour
{
public GameObject MergeBlob(GameObject originalBlob, GameObject newBlob)
{
Vector3 originalScale = originalBlob.transform.localeScale;
Vector3 newScale = newBlob.transform.localeScale;
Vector3 mergedScale = new Vector3(originalScale.x * (1 + newScale.x),
originalScale.y * (1 + newScale.y),
originalScale.z * (1 + newScale.z));
originalBlob.transform.localeScale = mergedScale;
return originalBlob;
}
}
hm... that's at least a start! Not sure whether I figured that out fully, but do I understand that correctly that I have to attach that script to a separate game object (I called it simply Blob $$anonymous$$anager) or should i copy the function into the script attached to the blob prefab? And besides that... I don't see where the merging part itself is ^^'
thanks for the help!
In your original question where you say you are looking to merge two GameObject's, the only thing you mention is their size in that you want to grow or scale the one GameObject by another. You were not specific in what you wanted to merge other than the scale/size.
I did however take into account the fact that you may want to merge so to speak properties from one GameObject to another with this text (you can also modify any other properties here).
To answer you other question I made the script so that it could be a standalone script and called but you can easily just copy the function and place it where you wand. I have added an update of the function I provided earlier with a new one as well as some comments
public GameObject $$anonymous$$ergeBlob(bool additive, GameObject originalBlob, GameObject newBlob)
{
Vector3 originalScale = originalBlob.transform.localeScale;
Vector3 newScale = newBlob.transform.localeScale;
Vector3 mergedScale;
if (additive)
{
// increase the scale of originalBlob by newBlob
mergedScale = new Vector3(originalScale.x + newScale.x,
originalScale.y + newScale.y,
originalScale.z + newScale.z);
}
else
{
// increase the scale of originalBlob by multiplying newBlob * originalBlob
mergedScale = new Vector3(originalScale.x * (1 + newScale.x),
originalScale.y * (1 + newScale.y),
originalScale.z * (1 + newScale.z));
}
originalBlob.transform.localeScale = mergedScale;
// merge any other properties of newBlob into originalBlob here and/or
// if newBlob and originalBlob has any component attached that you want merged
// do it by getting the indiviual componts of each and make the necessary merges
return originalBlob;
}
Ok, that helps me, since I can delete the smaller blob after the original blob gets bigger (to simulate that they are merging into one blob) but the part that I don't get now is, how to get them to know, when to merge together. They should merge when they are at a certain (very short) distance from each other. I guess I didn't elaborate on that enough in my original question, and I'm sorry for that. But at least I now have a way of calculating the size of the blobs that are merging, so thank you for that ^^
I suspect I'll have to try something with raycasts, to detect other blobs in close proximity, but I'm not sure how to do that... If you have a more efficient way to do that, let me know, but you already helped me a lot, so no hard feelings when I don't get an answer ^^
Thanks again, and have a nice day fine sir (or lady... I don't judge ^^)
Follow this Question
Related Questions
checking if gameobject exists 1 Answer
How to Serialize or Save a List of GameObjects 1 Answer
Spawn objects in a row with same spacing 0 Answers
How to change the shape of gameObjects 2 Answers
Accessing values on a gameobject 1 Answer