public PlanetEntity(GameEntity planetE) { Planet = planetE.planet; Position = planetE.position; Mass = planetE.mass; Health = planetE.health; if (planetE.hasCannon) { Cannon = planetE.cannon; } if (planetE.hasCooldownTimer) { CooldownTimer = planetE.cooldownTimer; } }
void OnTriggerEnter(Collider other) { Sticky otherSticky; //only player starts sticky, so run this on him. if(transform.GetComponent<Sticky>() == null){ return; } GameObject part = other.gameObject; //remove the trigger, Tom: I don't know why, but doing this makes all the balls hit and none stick. part.collider.isTrigger = false; Rigidbody otherBody = part.transform.GetComponent<Rigidbody>(); //I don't know why this happens sometimes, I assume it's because adding components isn't instant //either way, I'm just gonna return here in that case. if(otherBody == null){ return; } Transform t = transform; while(t.parent != null){ t = t.parent; } Rigidbody thisBody = t.GetComponent<Rigidbody>(); float otherMass = otherBody.mass; float newMass = thisBody.mass + otherMass; //set new rigidbody mass thisBody.mass = newMass; //centers of mass Vector3 otherCoM = t.InverseTransformPoint(otherBody.worldCenterOfMass); Vector3 thisCoM = thisBody.centerOfMass; //difference Vector3 CoMDiff = otherCoM - thisCoM; //set new center of mass thisBody.centerOfMass = thisCoM + (otherMass/newMass) * CoMDiff; //destroy other rigidbody, they are now combined Destroy(part.transform.GetComponent<Rigidbody>()); //add a sticky to the new piece otherSticky = part.AddComponent<Sticky>(); //get new parent part Sticky toBeParent = GetNearest(part.transform.position, t.GetComponent<Sticky>()); //add it into heirarchy so it is connected part.transform.parent = toBeParent.transform; //add to tree MassComponent mc = new MassComponent(otherMass, otherCoM); toBeParent.children.Add(otherSticky,mc); //if booster add the rigidbody for forces Boost b = part.transform.GetComponent<Boost>(); if(b != null){ b.body = thisBody; } // Hm. If this sticky has a layer and the other doesn't, the other should inherit it. // That means things the player sticks to should join the player layer. // TODO Reset the layer if things become unstuck. if (gameObject.layer != 0 && otherSticky.gameObject.layer == 0) { otherSticky.gameObject.layer = gameObject.layer; } }