Search Unity

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

Discussion in 'Editor & General Support' started by Firlefanz73, Jun 10, 2021.

  1. Firlefanz73

    Firlefanz73

    Joined:
    Apr 2, 2015
    Posts:
    1,316
    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:

    Code (CSharp):
    1.     public void Init(bool isHead)
    2.     {
    3.         _rigidbody = GetComponent<Rigidbody>();
    4.         if (_rigidbody == null)
    5.         {
    6.             _rigidbody = GetComponentInChildren<Rigidbody>();
    7.         }
    8.         IsHead = isHead;
    9.         if (IsHead)
    10.         {
    11.             // Spawn children
    12.             Rigidbody _last = _rigidbody;
    13.             for (int i = 0; i < 8; i++)
    14.             {
    15.                 GameObject child = Instantiate(this.gameObject, transform.position + Vector3.back * 2f, Quaternion.identity, this.transform.parent);
    16.                 child.GetComponent<SpacewormController>().Init(false);
    17.                 HingeJoint hj = child.AddComponent<HingeJoint>();
    18.                 //hj.anchor = Vector3.zero;
    19.                 //hj.useSpring = true;
    20.                 hj.axis = Vector3.one;
    21.                 hj.massScale = 0.1f;
    22.                 hj.connectedBody = _last;
    23.                 _last = child.GetComponent<Rigidbody>();
    24.             }
    25.         }
    26.         else
    27.         {
    28.             _rigidbody.mass = 0;
    29.         }
    30.     }
    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!
     
  2. Firlefanz73

    Firlefanz73

    Joined:
    Apr 2, 2015
    Posts:
    1,316
    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...

    Code (CSharp):
    1.                 if (IsHead)
    2.                 {
    3.                     _rigidbody.MovePosition(transform.position + Direction / 16f + Vector3.back / 48f);
    4.                 }
    5.                 else
    6.                 {
    7.                     float dist = Vector3.Distance(transform.position, Previous.transform.position) -1;
    8.                     Vector3 movePosition = transform.position;
    9.                     movePosition.x = Mathf.MoveTowards(transform.position.x, Previous.transform.position.x, dist);
    10.                     movePosition.z = Mathf.MoveTowards(transform.position.z, Previous.transform.position.z, dist);
    11.                     _rigidbody.MovePosition(movePosition);
    12.                 }
    13.  
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,748
    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.
     

    Attached Files:

    Firlefanz73 likes this.
  4. Firlefanz73

    Firlefanz73

    Joined:
    Apr 2, 2015
    Posts:
    1,316
    Sounds great, I will have a look tomorrow!

    Thanks :)
     
  5. Firlefanz73

    Firlefanz73

    Joined:
    Apr 2, 2015
    Posts:
    1,316
    Yes that's way more smooth :)
    Thanks a lot for that!
     
    Kurt-Dekker likes this.