AI detect incoming rigid body position placement to catch it
Hello, I need help figuring out two physics based scenarios within my game. I have searched high and low here and on Stack with none of the answers providing what I actually need nor a good solution. I apologize in advance if there is an undiscovered thread on my behalf.
Regardless, the first scenario is that in my game the AI must “catch” an incoming rigid body; I have blendtrees set up for the different animations (up, left, right, etc) depending on where the rigid body would be coming from. The issue is, actually getting the AI to be aware or know where the object would be relative to them when they are going to attempt a catch (this would depend on the velocity/angle/height of the incoming projectile). So I’ve been basically looking for a solution that would tell the AI exactly the point of intersection before the object is even there to know whether it fires an animation to lunge left to catch, or right, or jump, or middle down etc. including the fact if the object would be beyond their reach.
I’ve used physics simulation algorithms and all but they either are to slow or not accurate for what I need. I don’t know if it’s due to the fact that the object in question that I’m trying to solve for has a drag value of 0.336 and a mass of 0.146. I currently have it set up that the AI game object has a Distance SqrMagnitude code triggering when the object is near them or incoming to trigger the function and this works well to detect the incoming rigid body but I’m just missing the fine tune part of letting the AI know exactly where it will be once it reaches the AI in order to fire off the correct animation and the AI could actually catch it.
Side note, I have the code setup that once it enters the trigger sqrmagnitude range the catch animation fires and the AI does catch it BUT, the rigid body visually and collision wise will pass the AI which is in animation and then all of a sudden the object will appear in its hand. Therefore, if the object is in the air and the AI “catches” it, the object keeps going past it and hits the floor or continues past it and then just appears in its hand after. I have them both set to continuous collision detection and all yet it still happens.
Secondly, this part kind of ties in with the first but not exactly. I’m having an issue as well finding a real good put together solution to know exactly where a rigid body would land on the floor if it’s shot up in the air. So the AI navmesh would run to this spot to try and catch it before it lands; so I am trying to get the vector coordinates of that exact landing spot from as soon as an object is in motion. I’ve tried numerous solutions online with mixed results because most don’t even take into account, like before, the objects drag which I have at a 0.336 value with a mass of 0.146 just as before.
If anyone could help me out with this in any way, shape or form, including even pointing me to the right place online because I’ve honestly searched everywhere.
Lastly, really sorry about the length of the post lol.
Answer by lgarczyn · Nov 16, 2019 at 05:13 AM
To predict the position of the object, you simply need to increment the position by the velocity * deltaTime, and decrement the y velocity by gravity * deltaTime, and repeat for every frame you want to simulate. It's a actually cheaper than it sounds.
Calculating drag is a bit harder, by can be done: https://answers.unity.com/questions/652010/how-drag-is-calculated-by-unity-engine.html
Again, repeat every "simulated frame".
Once you know the position it will have when passing the target (can be done with raycasts), simply calculate the time it took to get there, and wait for that time minus your animation time to start the animation.
Once the AI catches the ball, make the rigidbody kinematic or disable it in some way.