Search Unity

Help To move objects simultaneous / Achieve R2D2 Leg Transition Simulation

Discussion in 'Scripting' started by JediNizar, Oct 27, 2021.

  1. JediNizar

    JediNizar

    Joined:
    Nov 13, 2016
    Posts:
    111
    Hi I'm not 100% sure on where to post this as it could ve "just" physics, animation or Scripting issue, so sorry for that if I posted this here wrongly.

    I'm creating an Interface for my 1:1 size R2D2 build (communication through BT over Pi and Arduino)
    anyway I need to simulate the 2-3-2 Legs Transition .
    FYI this is not mine just to show what I need to simulate,
    Unfortunately my R2D2 model is not rigged, and I don't know how to do it and even don't know if it would help.

    I have 10 game objects:
    R2D2 (Pivot point on center)
    ------->Dome (Pivot point on center)
    ------->Body (Pivot point on center)
    ------->Legs
    -------------->Left leg (Pivot point on shoulder)
    --------------------->Left foot (Pivot point on the ankle)
    -------------->Right leg (Pivot point on shoulder)
    --------------------->Right foot (Pivot point on the ankle)
    -------------->Center leg
    --------------------->Center foot (Pivot point on the ankle)

    The transition from 2 to 3 legs would be: (Dome + Body + Center Leg) rotate together 18° on the X axis
    at the same time the center leg move out to a certain position(until the center foot touch the ground)
    as soon the center foot touch the ground it needs to go forward until the 2 legs (left and right) build a 36° degree on their X axis (Pivot point on the shoulder), the feet (left and right) need to stay fix on the ground and only the legs rotate 36° degree on the shoulder and of course the Ankle needs to rotate accordantly.

    So basically I would like to know how to move objects simulant at the same time it would help, as I thought I use Coroutines, with something like this:
    Code (CSharp):
    1. void Update()
    2. {
    3.     if(Input.GetMouseButtonDown(0))
    4.     {
    5.         StartCoroutine(Set3LegMode());
    6.     }
    7. }
    8.  
    9. IEnumerator Set3LegMode()
    10.     {
    11.         while(Body degree != 18)
    12.         {
    13.             rotate Body();  ===>I would need to rotate all except the 2 legs left & right
    14.             moveOutCenterLeg(); ===> move the Center  in Y axis to certain by specific Offset
    15.             yield return null;
    16.         }
    17.  
    18.         rotateCenterFoot();
    19.  
    20.         while (LegShoulderXAxis degree != 36)
    21.         {
    22.             rotateLegs();
    23.             rotateAnkel();  ===> with an offset accordently to the shoulder rotation
    24.             yield return null;
    25.         }
    26.     }
     
  2. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    646
    So either you can do it by creating an Animation and just change your Animation state,
    if you want to change it via code you do can use coroutines with a callback when something is at its rotation, to react upon that

    if you start your coroutines via. update loop you will start multiple Coroutines prob., take care of that

    Small Edit:
    If you do work with a whole Human Bone Structure, then i do recommend to go with animations, but it really depends on how it should look when the state is changing