# Realtime inverse kinematics on unity apps for android and iOS

Hello I want to understand how much overheads do implementing inverse kinematics in realtime add.

I am looking at an implementation as such demonstrated in the video. https://www.youtube.com/watch?v=Gyakxv34404&feature=youtu.be

Let me explain the exact requirement now.

Assuming a game of mario, where mario is a 3D Character. And there is a coin placed at a height. So when mario comes into area of effect for the coin, we know the initial position of his arms, and we know the expected final position which is the position of the coin.

So we create a trajectory from initial to final position in realtime and the main character moves accordingly in the trajectory.

Now I can understand this would work fine in editor but if we have exported the game and this computations happen realtime for every object, how much CPU intensive would this be? Do all the existing IK assets provided on store

for example in this video of final IK https://www.youtube.com/watch?v=ZuWvGq3yV44 the characters are following the trajectory in real time I suppose. But what if we were to create the trajectory realtime depending on player positions, would that be a CPU intensive task to NOT work on mobile devices?

• "Moves accordingly" needs some unpacking. What's the expected movement for your game? If you just need the arm to reach out to grab the coin, that's trivial. If you need Mario's torso to move too, that needs a little more work. If you need to drive his whole locomotion off of IK, eg. figuring out where to place each foot as he walks/runs/jumps, that's more expensive yet. So, where on this spectrum is the problem you want to solve? Feb 23, 2017 at 13:09
• Last one. Basically the coin part was an example, I am looking at making it interact with multiple objects in a similar fashion. Like the trivial walk and etc animations would be pre-made but the more detailed ones such as climbing stairs, collecting objects, jumping in between platforms, killing enemies. those ones. the pattern for the trajectory (curve function) would be pre-defined. but depending on the position of main character and element the trajectory would form. Feb 23, 2017 at 13:22
• And…why? It's certainly not impossible, but it's a lot more complex to get it all working well than just dropping in some run & jump animations. See for example Overgrowth and all the layers in their procedural animation system — that's a lot of development effort to spend on something that might or might not improve over a basic animation controller available out of the box in Unity. But there's nothing in there that's so CPU intensive that it wouldn't work on mobile, as long as you're frugal with your physics checks Feb 23, 2017 at 13:34
• Can you please elaborate on your last point. "as long as you're frugal with your physics checks" Feb 23, 2017 at 13:49
• ie. Don't fire 20 raycasts each frame for every one of hundreds of characters navigating around. That would be not frugal. ;) Feb 23, 2017 at 13:53

As you can see in videos you've provided it is possible even for more characters. In the second video you have more than 10 models, each of them is computed in realtime and I suppose they don't share the same IK results (the reason for this demo is to show it can solve foot and hand contacts for different character proportions, so each character is computed separately).

The IK itself doesn't need to be so heavy. There are many levels of IK - you can compute a hand or leg made of just two segments or you can compute entire body at once. The first one is very simple computationally while the second is almost never needed in a game.

Having that said I'm not sure if you approach the problem from the good side or I didn't understand your description. What you wrote seems like you have the current position of hand and the target position, and you want to 'drag' your character by hand from start to end.

This could work but you may end up with other problems like breaking of the rest of the body movement. Instead, I would move the character by his center as always, and IK would just respond to this movement.

I'll give you another example from Mario game:

In old Mario game you could jump under the brick to hit it with your fist but because of the character proportions its hand ended at the same level as head so it looked fine. Now let's assume you put a typical human in place of Mario. You hit 'jump' and Mario raises his hand above head and starts moving up. At some point Mario will touch the brick and you need to decide if it keeps moving or stops - either way may look odd.

So what can we do with IK? On jump you set simple solver, only for the raised hand. The solver have the task to not to allow the hand to move above brick level. Then when you're movig up and touch the brick then you hand will start to bend, so it never goes above the brick. Given that all the details were done right it should improve the look of you character.

On the end few random thoughts I recall from working on IK in Unity:

• Bio IK is not the fastest one, it's more like an R&D. If it works for you then fine, if it gets too slow - change it to Final IK.
• Don't drag your character by hands/feet and depend on IK where possible. Instead move the character as always and use IK only to modify limbs movement.
• Try to avoid complex IK structures, like multiple chains, full-body IK etc. If possible use few separate limb solvers.
• Final IK has A LOT of nice examples, showing you how to achieve some specific effect using simple solvers. It's worth to look at, just to see how you can solve common problems.

If you only have one IK animation computation happening and it is not happening at every frame, then it should be fine in real-time -- especially if you are using FinalIK or BioIK. However, if you are worried about the performance of your IK solver, you can easily build your own FABRIK IK Solver: Build Your Own IK in Unity3D