Alright, so I’m trying to accomplish a sort of capture system. The player is going to throw a sort of “smart grenade”. If the grenade collides with anything that is not an enemy, the grenade will be destroyed. However, if the grenade collides with a game object with the tag Enemy, I want the grenade to then bounce off of the enemy, hover over the enemy, and explode releasing a net. I’m not looking for somebody to write the code for me, just looking for some suggestions on how to go about this. I already have the code written that allows the player to throw the grenade and I have the grenade destroying itself if it collides with something that’s not an enemy.
Raycast on the downward axis that triggers an event to capture when an enemy tagged “Enemy” is caught in it?
For the bounce off, it should be nearly automatic with the physics engine, as far as I understand it.
Hmmm. I guess one of the things that stumping me is where to put that in code. It would need to go inside the OnCollisionEnter function, but that function isn’t updated constantly… And using the raycast downward to make the grenade hover needs to be inside Update or FixedUpdate.
Set a variable with the colliding object in OnCollisionEnter(), then the update can start doing its calculations and animations only if there is a value. You should probably have a state machine of some sort there. If it’s set to the hovering state, animate until in position to cast a net, then change to that state etc.
I would spawn a coroutine after you have detected that you hit an enemy. That coroutine would just keep going until its done its stuff then bail out. Have you used coroutines before?
I’d take the easy way out… have an empty game object as a child of your enemy that’s above it’s head… on enemy hit, set your grenade to be a child of the enemy and set its position to that empty object’s position (lerp or your favorite smooth motion process to get it there without just appearing there). As long as it’s a child of the enemy (and you disable any rigidbody) it will remain at the position above their head even if they move.