I am trying to link two objects together using script. What is supposed to happen is that when an object collides with another objects they join together immediately on impact. However they are still able to perform different animals so they are basically not one object but look like one object. They are also supposed to move in a synchronized fashion. I have tried using SetParent but I don’t know if this could be an if statement or start function. So I just need a few suggestions on how to “link” objects with script. Thank you for your time.
GameObjectA.transform.parent = GameObjectB.transform;
I am assuming you want some form of “Katamari” effect. To do this (or something like it) the code is rather simple. To parent one object to another:
void OnCollisionEnter(Collision other)
{
if(other.gameObject.tag == "yourTagHere")
other.transform.parent = transform;
}
Now, if you want a more “Katamari” feel where you get a giant ball eventually, you would want one parent to be the “center” and all object share it. The code would look like:
void OnCollisionEnter(Collision other)
{
if(other.gameObject.tag == "yourTagHere")
other.transform.parent = transform.parent;
}
Both of these code samples assume that the “loose” object is the “other” (and would need to be tagged appropriately).