- Home /
Navmesh sometimes falling through world when enabled
So this one is baffling me. Right now I have a simple navmesh agent setup to track the player and just walk towards him. When the enemy is hit a small knockback force is applied, the enemy stops moving and the navmesh agent is disabled, once a timer counts down the navmesh reenables to follow the player. Before I used navmesh, I was just doing a basic rigidbody.velocity movement towards the player and it worked just fine ( with the exception of not being able to avoid objects)
Now with the navmesh agent going, every once and a while when I hit the enemy, it gets BLASTED through the floor on the first hit and flies off into space under the world(they should be able to take about 5 hits before they destroy)
then obviously they can't turn their navmesh back on because they are somewhere 1000 miles below the world lol.
I can't figure it out. I've tried changing when the navmesh disables, I've put the code in the hit box, before the add force, after the add force and it always does the same thing.
Here is a gif of what is happening
https://i.imgur.com/hNhRAHC.gifv
The "intended" behavior is that they get hit> stop moving> knocked back. They have 100 HP and each hit does 15 damage.
What you are seeing when they immediately disappear is them flying through the ground.
From what I can gather, it has something to do with navmesh stopping. I've tried updatePosition as well and the same thing happens.
Here is the enemy code
public class Enemy : MonoBehaviour
{
public Transform target;
private Transform player;
private Rigidbody rb;
public float spd;
public bool isHit;
private float hitStun;
public float hitTimer = 1;
public float hp;
public NavMeshAgent agent;
// Start is called before the first frame update
void Start()
{
agent = GetComponent<NavMeshAgent>();
rb = GetComponent<Rigidbody>();
player = GameObject.Find("Player").transform;
target = player;
}
// Update is called once per frame
void Update()
{
if (isHit && Time.time > hitStun)
{
isHit = false;
}
if (hp <= 0)
{
Destroy(gameObject);
}
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Weapon"))
{
hitStun = Time.time + hitTimer;
isHit = true;
}
}
private void FixedUpdate()
{
if (!isHit)
{
agent.enabled = true;
if (agent.enabled)
{
agent.destination = target.transform.position;
}
}
}
public void TakeDamage(float dmg)
{
hp -= dmg;
}
}
and here is the hit box code that uses the trigger to hit the enemy
public class hitBox : MonoBehaviour
{
public float knockback;
private Rigidbody rb;
public Transform playerPos;
private Enemy enemy;
public float dmg;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Enemy"))
{
enemy = other.GetComponent<Enemy>();
rb = other.GetComponent<Rigidbody>();
Vector3 direction = (other.transform.position - playerPos.position).normalized;
enemy.agent.enabled = false;
enemy.TakeDamage(dmg);
rb.AddForce(new Vector3(direction.x, 0, 0) * knockback);
}
}
}
I've tried everything I can think of. I noticed if I set the enemy spawner higher into the air, the enemies slam into the ground upon spawning, so it looks like they aren't using normal gravity, just snapping to the terrain. I am wondering if that might have something to do with it.
Your answer
Follow this Question
Related Questions
How can i check if GO collided with collider starting with name (string) 1 Answer
How can I have an enemy be able to fall through a Navmesh? 0 Answers
GameObjects glitching through other Objects when being held 0 Answers
Enemy Object sometimes falls through floor on hit, only with Navmesh 0 Answers
Gameobject collision with Terrain C# 4 Answers