The timer in my game is supposed to wait till the timer reaches a time, then the enemy attacks, it works fine in Unity3d, but not in the standalone game itself, instead of wait 2 seconds for every attack it will attack every like 0.05 seconds but it works fine in editor just buggs when build and I play the build.
My Script:
using UnityEngine;
using System.Collections;
public class Zombie : MonoBehaviour {
public Transform Player;
public GameObject _zombie;
public float Movespeed = 4.35f;
public float MaxDistance = 55;
private float CurrentHealth;
private float MaxHealth = 100;
private float AttackTimer = 1.7f;
private bool hasMoved = false;
public AudioClip ZombieSound;
public GameObject Ragdoll;
void Start () {
CurrentHealth = MaxHealth;
Player = GameObject.FindGameObjectWithTag("Player").transform;
_zombie = gameObject;
CharacterController controller = GetComponent();
}
void Update () {
AttackTimer-=Time.deltaTime;
CharacterController controller = GetComponent();
PlayerScore PS = Player.GetComponent();
/* if(hasMoved == false){
controller.SimpleMove(_zombie.transform.forward * 3);
hasMoved = true;
}*/
if(AttackTimer <= 0.1f){
Attack();
}
if(CurrentHealth < 1){
PS.Score++;
Instantiate(Ragdoll,transform.position,transform.rotation);
Destroy(gameObject);
}
float dist = Vector3.Distance(Player.position, _zombie.transform.position);
transform.LookAt(new Vector3(Player.transform.position.x, transform.position.y,Player.position.z));
//CharacterController controller = GetComponent();
controller.SimpleMove(_zombie.transform.forward * Movespeed);
}
void ApplySwordDamage(float Damage){
CurrentHealth -= Damage;
}
void Attack(){
float dist = Vector3.Distance(gameObject.transform.position,Player.position);
PlayerHealth health = Player.GetComponent();
if(dist <= 1.5f){
health.CurHealth-=5;
animation.CrossFade("ZombieAttack_1");
audio.PlayOneShot(ZombieSound);
AttackTimer = 2.2f;
}
}
}