I recently went back to one of my older projects, and it’s a re-make of the raft style games, where you start with one raft, and if you bump into another one it attaches and your raft gets bigger. Along with other added characteristics(speed, damage, etc…).
At first I was just using normal transform manipulation, and childed the new rafts to the given “locations” of snapping, but eventually got all twisted up on making my own physics. Not to mention needing a rigidbody anyway for proper collisions, I eventually scrapped making my own physics and chose to use the physics system.
But soon ran into many issues with the parent and children all having rigidbodys, as it prevented any movement. I’ve been scouring the web for any related situations and best found that FixedJoints were needed. And with small scale testing, this function seem to work:
void OnCollisionEnter(Collision col)
{
Vector3 direction = col.transform.position - trans.position;
if (Physics.Raycast(trans.position, direction, out RaycastHit hit, 10.0f))
{
if (hit.transform.TryGetComponent<Raft>(out Raft raft))
{
team = raft.team;
raft.myRafts.Add(this);
trans.position = raft.trans.position + hit.normal;
FixedJoint fix = gameObject.AddComponent<FixedJoint>();
fix.connectedBody = raft.rig;
}
}
}
Testing seemed to be perfect for my use case, until stress testing showed that certain angles or too many joints were breaking. Also each addition of a new raft, caused all other joints to move therefor re-calculating their connections.
So I added way more checks to prevent any additional hits, if a side was already taken or if the pieces in question were already on the main raft collection. But still bumping into other rafts sometimes caused the FixedJoints to break away in certain areas.
So I came to the conclusion that maybe I don’t want these collected rafts to move at all(from physics) once applied to the main raft, basically leaning me back into maybe childing them to the main raft. This is hoping that the FixedJoints won’t cause any issues with allowing the main to AddForce(or direction to move) since they’re technically jointed to the parent(main) rigidbody.
But then I also thought maybe just declare their transform position each frame, once they joint up, and move them(or keep them placed) while the main moves with physics. But I feel like manually setting their transform positions each frame(even if with a manager of sorts), would be too performance heavy. Or maybe even using a ton of joints themselves is too performance heavy. not sure…
So my question is, is there a better way to do this? Or does someone have an example, or know of a tutorial doing something similar? Or basically am I stuck to just childing the rafts and keeping them jointed? Or just make my own physics system like I had at the start?
Note: My older way would delete the rigidbodys on a raft that gets added/collected, preventing the multiple rigidbody setup. But I think there was an issue with rafts clipping into each other, and I felt that deleting and adding rigidbodys back(if owner died, or pieces broke off from combat) was a horrible way to go about it. Especially with needing their declared mass(heavy/slow raft pieces).