Spaceworm / Centipede made of single spheres (like a pearl necklace) -> HingeJoints?

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!

Works okay wihout HingeJoints, but not perfect.
I put the previous part into Prevoius and one is the head. Going slowly down (=back) and having different directions the head moves, the others follow. Could be better but already working okay…

                if (IsHead)
                {
                    _rigidbody.MovePosition(transform.position + Direction / 16f + Vector3.back / 48f);
                }
                else
                {
                    float dist = Vector3.Distance(transform.position, Previous.transform.position) -1;
                    Vector3 movePosition = transform.position;
                    movePosition.x = Mathf.MoveTowards(transform.position.x, Previous.transform.position.x, dist);
                    movePosition.z = Mathf.MoveTowards(transform.position.z, Previous.transform.position.z, dist);
                    _rigidbody.MovePosition(movePosition);
                }

I would not use physics or joints or anything like that for following. I think it would be insanely difficult to get it to be nice and smooth as folks expect from a 3D shooter.

Instead, use a Bezier spline following utility with offsets, there’s a few on the asset store.

You’re also welcome to use my “FollowEnTrail” system, attached below.

You make a single thing that you move however you want, and you make any number of “clients” that follow it en trail, either spacing by time or spacing by distance.

Demo of both included. Turn off one or the other client type (in TestFollowEnTrail.cs) to see the other more clearly.

7227238–869521–FollowEnTrail.unitypackage (4.51 KB)

1 Like

Sounds great, I will have a look tomorrow!

Thanks :slight_smile:

Yes that’s way more smooth :slight_smile:
Thanks a lot for that!

1 Like