Search code examples
3dunity-game-enginegame-physics

Catching Ball using a humanoid model in unity?


In unity what approach to use if we want a human player to catch a ball thrown at him. I have animations for catch but the animations will require to be sycchronised in such a way that when the animation plays automatucally a little while before the ball reaches the players hand such that when the ball reaches the hand the animation is in a frame where the player as ready to recieve the ball and also how to change the position of hand and player so that it can catch a various types of ball(ball may be comming from slightly trajectory and recived at diffrent heights) comming at him and the catch doesnt look un natural.

I have diffrent animations for catching. I am ok with coding. I dont need exact code . what i require is what should be the logic to make a model with fixed no of catch animations to catch every ball comming towards his direction perfectly.


Solution

  • You will need to leverage inverse kinematics along with your animations to do this.

    Unity provides built-in support for this feature, but it is a Pro-only feature. The Asset store has a number of add-ons to provide the feature in Unity Free as well, one of which is free.

    You can also implement your own IK system. There are numerous sources on the internet on how to do so, including this video tutorial for C++ (the concepts and math carry over to any language).

    The key to fluid motions is to mix your current animations with your IK solver. How you do this depends on how you choose to implement your IK system, but the logic will look something like:

     if(ball.angleFromPlayer more than 90 or less than 180)
            playAnimationA()
     if(ball.angleFromPlayer more than 180 or less than 270)
            playAnimationB()
     ...
    
     mixingPercentage = ball.distance/ball.startingDistance;
     solveIKForArms(ball.postion,mixingPercentage)