- Home /
Knockback using Navmesh
Hey guys!
Another question from me,
I'm trying to simulate knocking back an enemy when they are hit by the "box collider" I have attached to the players weapon. I was just wondering how I could push the enemy backwards, away from the player using a Navmesh command/function rather than using Addforce, which is what I have implemented currently. As it throw my enemy's off of the navmesh and they slide around.
Any and all help is appreciated!
Thanks!
Answer by sparkzbarca · Oct 30, 2012 at 07:01 AM
set the destination equal to a point x distance along the vector goes from player to ai.
direction = ai.position - player.position;
distance = 1; destination = direction * distance
this would make the destination 1 unit (a unit in unity is 1 meter) or approx 3 feet away from the player in the direction of the ai character.
Assuming the players are scaled right (a normal sized human is approx 2 units or 6 feet in height) and the ai is standing very close to the player the ai would move to a position just under 3 feet away. Thats one way to do it though it kinda makes the AI run there if thats the animation its under. You'd need a knockback animation that shows him going through the air or however you want him to look when knocked back and orient it with the front towards the player so that as he moves toward the destination he still faces you and APPEARS to be knocked back, if you bump up the speed to fast and give it the right animation he'll move quickly backwards while facing you and basically he'll be knocked back.
You said he was knocked out of the navmesh why exactly? does the nav mesh not fully encompass your terrain? how is he getting knocked off it?
They seem to be getting knocked off of the Navmesh, because we're currently detecting our collision, by using a rigidbody and a capsule collider and we're using Addforce on it, in order to knock our enemies around. For some reason, it's causing our enemies to slide around sometimes when we attack them and it's the only explanation I can think of, for why they won't stop sliding after being hit.
view navmesh and pause after knockback to see if they leave the navmesh. still sliding might mean the force isn't at some point being removed, like they have a low friction coeffiecent. $$anonymous$$aybe they slide so much because they are in fact slippery? Unfortunately I do not own unity Pro so i cannot help you with Nav$$anonymous$$esh that much.
Do they still slide when you create an empty terrain completely covered in navmesh? If so you can rule out bla$$anonymous$$g the navmesh
If they dont your correct its the navmesh and you'll need to restrict movement to along the navmesh
You said SO$$anonymous$$ETI$$anonymous$$ES they slide. I assume that means sometimes they dont. debug log the force on them. maybe its being continually added sometimes.
Thank you. It's been fixed based on what we discussed the problem was in fact the nav mesh, but it was because they were set as kinematic rigidbodies before and they were being set to false.
We went back into our collision code and made sure our code was re-enabling the nav mesh agent and making them kinematic once again, so they stopped sliding around.
You said you were using addforce but using $$anonymous$$inematic rigidbodies?
Thats wierd how is that working?
$$anonymous$$inematic means it doesnt use the physics engine. I guess are you disabling kinematic, knocking them back, then making them kinematic again to remove the force?
just to note you could use a slerp move to avoid changing there kinematic status.
$$anonymous$$inematic objects are nice except that they only collide when placed within the fixed update.
A charactercontroller is probably more of what you want here since its probably a traditional AI.
Well whatever works for you. :)
Yeah that's exactly what we're doing. We're also disabling the Nav $$anonymous$$esh agent before we set their kinematic value to false. Then after they've been knocked the distance we want, we set their kinematic bool to true and re-enable the nav mesh agent. :) It works pretty decently, and we're not getting any errors.
You just have to make sure you do checks, so that you aren't calling any nAgent commands such as Stop() or SetDestination() while the nAgent is disabled or you'll get NullReferences.