Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Updating animated 2d IK target

Discussion in '2D' started by rheirman, Feb 4, 2021.

  1. rheirman

    rheirman

    Joined:
    Sep 30, 2019
    Posts:
    34
    I have a 2d skeletal animation set up where 2D Inverse Kinematics package (https://docs.unity3d.com/Packages/com.unity.2d.ik@1.1/manual/index.html) for controlling the arm and leg movement of my character while he's walking.

    When the character is aiming and shooting, I'd like the IK to point towards the target, instead of adhering to walk animation I set up. I tried doing this by just changing the IK target's position in LateUpdate. However, this does not update the skeleton of my character (which makes sense, since LateUpdate is called after all the animations and skeleton updates are performed). Is there any way I can update the IK position at the right time? So after the animation changes the IK target position, but before the skeleton changes are applied? Or is there any way to force the skeleton to be updated after changing the IK target position?

    An alternative solution would be to clone the walk animation that I have, but remove any changes it applies to the IK of the arm that should aim, and use that new animation for aiming. This way I can just update the arm IK target without worrying that the animation will overwrite my changes. However, this doesn't seem very elegant, as it requires me to duplicate my animations. Any other suggestions on how to solve this?
     
    Last edited: Feb 5, 2021
  2. rheirman

    rheirman

    Joined:
    Sep 30, 2019
    Posts:
    34
    I figured this out. The solution was surprisingly simple. I just created two IK targets. One is used in the normal animations, and once the character aims, I swap the IK target referenced by the solver by the aim IK target.
     
  3. OsmnZeki

    OsmnZeki

    Joined:
    Dec 2, 2019
    Posts:
    1
    Same problem but I cant understand your explanation. How did you access target to change targets via script ? I can't access target with script. Thanks for your help
     
  4. mzlafty

    mzlafty

    Joined:
    Jan 20, 2020
    Posts:
    3
    Yeye, target can't be found in CCDSolver2D. I'm trying to find a solution right now though.

    Edit: Ey, CCDSolver2D has a GetChain() method, which takes in a chain index. There is only one chain in the CCDSolver, and you just define the chain length, so I threw in 0 for the parameter, and then I was able to get the target from that, but ye. I did the same thing rheirman did. I made an IK target that was a child of the object (controlled by the animator), and I made an IK target outside of the object to control in code, then I swapped the references to them in the animations.
     
    Last edited: Jun 11, 2021
  5. rheirman

    rheirman

    Joined:
    Sep 30, 2019
    Posts:
    34
    Sorry, I completely missed your reply to this thread.

    Yeah, the explanation of mzlafty is indeed how I did it.

    My code looks something like this:
    Code (CSharp):
    1.    
    2. public void EnableDefaultTarget()
    3.     {
    4.         DefaultTarget.SetActive(true);
    5.         var chain = Solver.GetChain(0);
    6.         chain.target = DefaultTarget.transform;
    7.         AimTarget.SetActive(false);
    8.     }
    9.  
    Similar code handles the enabling of AimTarget here.


    DefaultTarget and AimTarget are GameObjects that are set through the editor. The editor looks like this:

    upload_2021-6-27_13-50-2.png

    I hope this still helps someone.
     
  6. colbydanesaddoris

    colbydanesaddoris

    Joined:
    Nov 3, 2018
    Posts:
    5
    Another option is to create another Solver with the same chain and create a new target that is not included in your animation clip. Using a script ( or editor ) you can change the weight of the "New Aim" solver to blend between the animation and Aiming IK..
     
    bigy likes this.
  7. thezzw

    thezzw

    Joined:
    Jan 19, 2022
    Posts:
    2
    thanks a lot for this solution
     
  8. jester74864

    jester74864

    Joined:
    Aug 2, 2018
    Posts:
    10
    For anyone else who stumbles along here, you need the right namespace:
    using UnityEngine.U2D.IK;
    Then suddenly, the world of IK opens up.