replaced system
unity nav mesh problems
so idk if this is actually a nav mesh issue, but i believe it may be part of the cause. so i have an ai im using as a boss for my mobile game. the ai is supposed to stop when hit, and increase its speed after health reaches half. and it kinda works but heres the problem. when it reaches half health, the speed is increased. but for some reason the health goes down extremely quickly and the speed can't be set 0 anymore after being hit. there are other problems such as collisions not working properly and the player dying randomly but they are not my main focus, as i think they will be fixed simultaneously.
im using 2 scripts. one to control the nav mesh and speed, and the other to control health. the health script also deals with most of the speed changes out of 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;
}
}
Follow this Question
Related Questions
problems with nav mesh agent 0 Answers
Mobile Optimization question 1 Answer
Need some Help with Swipes! 0 Answers
c# Mobile Joystick issues - implementing joystick controls into movement and camera script 0 Answers
Not working Update() on android 0 Answers