- Home /
Going to rephrase question with some updated info
Enemy Object sometimes falls through floor on hit, only with Navmesh
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.
The enemy code looks like this
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.AI;
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 the hitbox code looks like this
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);
}
}
}
Any ideas would be greatly appreciated. I've been messing with this for a while and just can't figure it out.