problems with nav mesh agent
so im making a boss ai for my mobile game. its super simple since its the first ive tried to make a boss system. it has 4 floats that control the speed. moveSpeed which is not set to any value, defaultMoveSpeed which is the default value for moveSpeed as initialized on start, hurtSpeed which is what move Speed is set to when hit, and fastSpeed which is set when health goes below half.
defaultMoveSpeed = 1.5f;
moveSpeed;
hurtSpeed = 0f;
fastSpeed = 2f;
the problem is when the health goes below the half mark, it dosnt function properly. the health drains very quickly and for some reason it drains the player quickly, but inconsistently. im not sure but i think the ai speed is being set higher than 2, as it seems like it moves faster when health goes below half. last thing is the speed can no longer be set to hurt speed after half health. there are 2 scripts applied to the ai. the movement and the health scripts. movement has the navmesh functions in it, and the health obviously deals with health. it also deals with speed switching for convenience.
here are the scripts
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class AikoKumi : MonoBehaviour { public float lookRadius = 20f;
public float defaultMoveSpeed = 1.5f;
public float moveSpeed;
public float hurtSpeed = 0f;
public float fastSpeed = 2f;
public Transform target;
NavMeshAgent agent;
// Use this for initialization
void Start ()
{
agent = GetComponent<NavMeshAgent>();
moveSpeed = defaultMoveSpeed;
}
// Update is called once per frame
void Update ()
{
GetComponent<NavMeshAgent>().speed = moveSpeed;
float distance = Vector3.Distance(target.position, transform.position);
if(distance <= lookRadius)
{
agent.SetDestination(target.position);
if(distance <= agent.stoppingDistance)
{
FaceTarget();
}
}
}
void FaceTarget()
{
Vector3 direction = (target.position - transform.position).normalized;
Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * 5f);
}
void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, lookRadius);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class AikoHealth : MonoBehaviour {
public float startHealth = 500f;
public float health;
public PlayerMovement playerMovement;
public AikoKumi aikoKumiCS;
[Header("Dont touch!")]
public Image healthBar;
public int dead = 0;
// Use this for initialization
void Start()
{
health = startHealth;
playerMovement = FindObjectOfType<PlayerMovement>();
}
// Update is called once per frame
void Update()
{
healthBar.fillAmount = health / startHealth;
if(health <= 251)
{
SetFastSpeed();
}
if (health <= 0)
{
Dying();
}
}
public void OnTriggerEnter(Collider col)
{
if (col.gameObject.name == "SwordCollider")
{
health -= playerMovement.damage;
aikoKumiCS.moveSpeed = aikoKumiCS.hurtSpeed;
if(health >= 251)
{
Invoke("ResetMoveSpeed", 2f);
}
if (health <= 251)
{
Invoke("SetFastSpeed", 2f);
}
}
}
void Dying()
{
dead = 1;
Invoke("Dead", 2);
}
void Dead()
{
if (dead == 1)
{
Destroy(gameObject);
}
}
public void ResetMoveSpeed()
{
aikoKumiCS.moveSpeed = aikoKumiCS.defaultMoveSpeed;
}
public void SetFastSpeed()
{
aikoKumiCS.moveSpeed = aikoKumiCS.fastSpeed;
}
}
im new to the nav mesh, though im not sure if this even the problem. any input would be very appreciated.
Your answer
Follow this Question
Related Questions
Having problems with making a Navmesh Agent stop and play an attack animation 0 Answers
ai patrol and chase question 0 Answers
Creating prefab through Zenject Factory makes navMeshAgent act strange 0 Answers
ProBuilder-made object not generating Nav Mesh (Unity3D) 1 Answer
NavMeshAgent isn't chasing the player 0 Answers