Hello,
I am working on an arcade space shooter with 3d gfx but 2d gameplay, a top down space shooter.
My 1st idea was to spawn the head, which then spawns all the other parts, all upcoming part is connected to the previous with a hinge joint. But it does not work, they are jumping around and they are only moving if I set all rigidbodies except of the head to mass 0. And if I turn off the collider or else they very quickly dissappear of the screen because their colliders hit…
My code to add a head which adds the other parts and connects them:
public void Init(bool isHead)
{
_rigidbody = GetComponent<Rigidbody>();
if (_rigidbody == null)
{
_rigidbody = GetComponentInChildren<Rigidbody>();
}
IsHead = isHead;
if (IsHead)
{
// Spawn children
Rigidbody _last = _rigidbody;
for (int i = 0; i < 8; i++)
{
GameObject child = Instantiate(this.gameObject, transform.position + Vector3.back * 2f, Quaternion.identity, this.transform.parent);
child.GetComponent<SpacewormController>().Init(false);
HingeJoint hj = child.AddComponent<HingeJoint>();
//hj.anchor = Vector3.zero;
//hj.useSpring = true;
hj.axis = Vector3.one;
hj.massScale = 0.1f;
hj.connectedBody = _last;
_last = child.GetComponent<Rigidbody>();
}
}
else
{
_rigidbody.mass = 0;
}
}
Is there a chance to get this working with rigidbodies and hinge joints?
Or should I think about just spawn them and have no hoint but let each part just move to the previous part and in both cases just move the head?
Thanks a lot!