How to make this enemy script faster? Any ideas?
Thank you in advice
void Update()
{
if (!animZ.GetCurrentAnimatorStateInfo(0).IsName("TZ_emerge") || isDead)
{
if (nav.isStopped == true)
nav.isStopped = false;
}
if (inRange && !animZ.GetCurrentAnimatorStateInfo(0).IsName("TZ_emerge"))
{
if (!playerStats.playerDead && !isDead)
{
if (Vector3.Distance(transform.position, player.position) <= aggroRange)
{
if (isSleeping)
{
animZ.SetBool("isSleeping", false);
aggroRange = aggroTemp;
isSleeping = false;
}
if (animZ.GetCurrentAnimatorStateInfo(0).IsName("RaiseFromDead") && animZ.GetCurrentAnimatorStateInfo(0).normalizedTime <= 0.1f)
{
audioSource.PlayOneShot(zomb_wakeup);
}
if (animZ.GetCurrentAnimatorStateInfo(0).IsName("RaiseFromDead") && animZ.GetCurrentAnimatorStateInfo(0).normalizedTime <= 2f)
{
nav.isStopped = true;
}
else if (!animZ.GetCurrentAnimatorStateInfo(0).IsName("SleepingZombie") || isDead)
{
if (nav.isStopped == true)
nav.isStopped = false;
}
animZ.SetBool("isAggro", true);
if (Vector3.Distance(transform.position, player.position) <= chaseRange)
{
animZ.SetBool("chasePlayer", true);
nav.speed = .37f;
}
else
{
animZ.SetBool("chasePlayer", false);
nav.speed = .24f;
}
}
else
{
animZ.SetBool("isAggro", false);
nav.isStopped = true;
Debug.Log("Navmesh Stopped for other reason == TRUE");
}
}
if (!isDead)
{
if (!playerStats.playerDead && !animZ.GetCurrentAnimatorStateInfo(0).IsName("TZ_emerge"))
{
nav.SetDestination(player.position);
}
animZ.SetFloat("Speed", Mathf.Abs(nav.velocity.x) + Mathf.Abs(nav.velocity.z));
float distance = Vector3.Distance(transform.position, player.position);
Vector3 direction = (player.position - transform.position).normalized;
Quaternion lookRotation = Quaternion.LookRotation(direction);
// Combat range rotation
if (distance <= 1 && !isDead)
{
if (!playerStats.playerDead)
{
animZ.SetBool("Attack", true);
nav.isStopped = true;
if (animZ.GetCurrentAnimatorStateInfo(0).IsName("ArmAttacks"))
transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * 0f);
else
transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * 10f);
}
}
else if (distance > 1)
{
animZ.SetBool("Attack", false);
}
else if (distance > 1 && Vector3.Distance(transform.position, player.position) <= aggroRange)
{
nav.isStopped = false;
}
if (canPoise && distance < poisonDistance)
{
poisonAttackTimer += Time.deltaTime;
if (poisonAttackTimer > poisonAttackInterval)
{
int tryToPoise = Random.Range(0, 100);
if (tryToPoise <= poisonChance)
{
player.SendMessageUpwards("PlayerPoisoned", poisonDamage, SendMessageOptions.RequireReceiver);
player.SendMessageUpwards("PoiseDuration", poisonDuration, SendMessageOptions.RequireReceiver);
}
poisonAttackTimer = 0;
}
}
if (playerStats.playerDead)
{
readyToFeast = true;
animZ.SetBool("killedPlayer", true);
Vector3 directionDead = (playerRoot.position - transform.position).normalized;
Quaternion lookDeadRotation = Quaternion.LookRotation(directionDead);
transform.rotation = Quaternion.Slerp(transform.rotation, lookDeadRotation, Time.deltaTime * 10f);
transform.rotation = Quaternion.Euler(new Vector3(0f, transform.eulerAngles.y, 0f));
if (Vector3.Distance(transform.position, playerRoot.position) >= 1.2f && (Vector3.Distance(transform.position, playerRoot.position) <= 3f))
{
transform.position = Vector3.MoveTowards(transform.position, playerRoot.position, Time.deltaTime / 20);
Debug.Log("Player Distance = " + Vector3.Distance(transform.position, playerRoot.position));
animZ.SetBool("moveToCorpse", true);
nav.isStopped = false;
nav.speed = .37f;
}
else
{
animZ.SetBool("moveToCorpse", false);
nav.isStopped = true;
}
if (readyToFeast)
{
feastStartTimer += Time.deltaTime;
}
if (feastStartTimer > 4)
{
nav.SetDestination(playerRoot.position);
transform.position = Vector3.MoveTowards(transform.position, playerRoot.position, Time.deltaTime / 10f);
feastStartTimer = 0;
readyToFeast = false;
}
}
if (canPoise)
{
if (distance < poisonDistance)
{
attackTimer += Time.deltaTime;
}
else if (attackTimer > 0)
{
attackTimer -= Time.deltaTime * 2;
animZ.SetBool("Attack", false);
}
else
{
attackTimer = 0;
animZ.SetBool("Attack", false);
}
}
}
if (!isSleeping && !IdleAudio.isPlaying && health > 0)
{
IdleAudio.Play();
}
if (hit && !isSleeping)
{
animZ.Play("ZTakeDamage");
hit = false;
}
if (health <= 0)
{
nav.isStopped = true;
if (!isDead)
{
audioSource.PlayOneShot(zomb_deathScream);
audioSource.PlayOneShot(zomb_deathCracks);
IdleAudio.Stop();
Death();
}
}
if (isDead && !canBeLooted)
{
lootTime();
//lootCollider.gameObject.layer = 26;
}
if (startSpellTimer)
{
timedArrackTimer += Time.deltaTime;
}
if (timedArrackTimer > 1f)
{
float damageN = playerStats.playerMagicDamage / 20;
ApplyDamage(damageN);
Debug.Log("HIT ENEMY ZOMBIE with timed Magic " + damageN);
timedArrackTimer = 0;
}
if (inScene && addOnlyOnce)
{
addOnlyOnce = false;
EnemySpawner.enemyCount.Add(this);
}
}
}