Search Unity

Question Issue with moving limbs to points

Discussion in 'Physics' started by itsdawg43, Dec 26, 2023.

  1. itsdawg43

    itsdawg43

    Joined:
    Mar 7, 2023
    Posts:
    16
    Heyo,
    I recently worked up a system to move the limbs of an enemy in my game using points that rotate around another point relative to the body via animation, then moving the end of the limb to the position of the target point, with the idea that it would drag with the rest of the limb that's connected to the main body via hinge joint. However, the points don't snap immediately, and only do so after the body has been idle for a short amount of time. Video here:

    As you can see, the red points are the positions where the feet should be, but they don't snap immediately. Here's the code I have to make them move:
    Code (CSharp):
    1. [System.Serializable]
    2. private class Leg
    3.     {
    4.         public Rigidbody2D touchPoint;
    5.         public Transform desiredPos;
    6.         public Leg(Rigidbody2D touchPoint, Transform desiredPos)
    7.         {
    8.             this.touchPoint = touchPoint;
    9.             this.desiredPos = desiredPos;
    10.         }
    11.     }
    12.  
    13. [SerializeField] Leg[] legs;
    14.  
    15. private void Start()
    16.     {
    17.         StartCoroutine(MoveLegs());
    18.     }
    19.  
    20. IEnumerator MoveLegs()
    21.     {
    22.         while (true)
    23.         {
    24.             foreach (Leg leg in legs)
    25.             {
    26.                 leg.touchPoint.position = leg.desiredPos.position;
    27.             }
    28.             yield return new WaitForFixedUpdate();
    29.         }
    30.     }
    This is a very specific issue and I may just end up re-formatting the whole system, but if anyone has some pointers or ideas then it would be greatly appreciated. Thank you!