- Home /
NavMeshAgent sliding/accelerating knockback, not teleport
So I want to get my enemy get pushed away from the bullet shooting point on impact, but the rigidbody addforce basically teleports the enemy instantly instead of enemy sliding away... Any ideas?
private void OnTriggerEnter(Collider other)
{
if(other.gameObject.layer == 10)
{
Knockback();
}
}
private void Knockback()
{
Debug.Log("knockback");
Rigidbody rigidBody = GetComponent<Rigidbody>();
navMeshAgent.enabled = false;
rigidBody.isKinematic = false;
Vector3 test = new Vector3(2500, 0, 0); // currently just pushes to the right for testing purposes
rigidBody.AddForce(test * rigidBody.mass, ForceMode.Acceleration);
//navMeshAgent.enabled = true;
//rigidBody.isKinematic = true;
}
Answer by MHernandezOlmo · Jul 23, 2020 at 11:42 AM
Hi @kot2202, forget about the mass,
If you are adding force only in one frame you just have to use Forcemode.Impulse:
rigidBody.AddForce(test, ForceMode.Impulse);
I found out that sliding knockback works on my enemies that have no Animator component for some reason. Then I disabled the root motion for it to work, no idea why, but it somehow overwrited the velocity to 0 almost instantly. Now it works. Also, do you by any chance, have an idea how and when to reenable the navmeshagent and isKinematic?
rigidBody.AddForce(test, Force$$anonymous$$ode.Impulse);
float stop = $$anonymous$$athf.Abs(rigidBody.velocity.x);
Debug.Log(stop);
if (stop < $$anonymous$$athf.Epsilon)
{
Debug.Log(stop);
nav$$anonymous$$eshAgent.enabled = true;
rigidBody.isKinematic = true;
}
the velocity change is kinda laggy and actually doesn't change instantly, the stop value goes through and if passes with a value of 0, it results it almost no knockback this way, coroutine would work but I would prefer it not being time based