// Update is called once per frame void Update() { int reee; if (can_move == !CanMove) { reee = 3; } if (is_dead) { return; } if (hp < 0.0001) { hp = 0; gameObject.AddComponent <DieAndFall>().Duration = 2.0f; can_be_attacked = false; //destroy hp bar Destroy(hp_bar); Destroy(gameObject.GetComponent <NavMeshAgent>()); Destroy(gameObject.GetComponent <CapsuleCollider>()); Destroy(gameObject.GetComponent <NavMeshObstacle>()); is_dead = true; return; } //set hp bar position //hp_bar_back.transform.position = cam.WorldToScreenPoint(transform.position); hp_controller.SetPosition(transform.position + Vector3.up * HealthBarHeight); //update the hp bars hp hp_controller.SetHpAndMaxHp(hp, MaxHp); //decide whether we can attack the player or noy //set attack target to something in range if we dont have one if (can_attack && heading_target == null && (target == null || target.IsDead())) { //get the units in range and choose one randomly List <unit_control_script> units_in_range = GlobalManager.GetGlobalManager().GetLevelManager().GetPlayerUnitsInRange(GetPosition(), DetectionRange); if (units_in_range.Count > 0) { heading_target = units_in_range[Random.Range(0, units_in_range.Count)]; } else { heading_target = null; } } //Check to see if there are any units in attack range if (can_attack && (target == null || target.IsDead())) { List <unit_control_script> units_in_range = GlobalManager.GetGlobalManager().GetLevelManager().GetPlayerUnitsInRange(GetPosition(), AttackRange); if (units_in_range.Count > 0) { target = units_in_range[Random.Range(0, units_in_range.Count)]; } } if (can_attack && target != null) { SetAttackOrder(target); } else if (can_attack && heading_target != null) { SetAttackOrder(heading_target); } ////get the nav agent //if (can_attack && nav_agent == null) // nav_agent = GetComponent<NavMeshAgent>(); //set the movespeed and acceleration if (can_move) { nav_agent.speed = MoveSpeed / 10; nav_agent.angularSpeed = MoveSpeed; nav_agent.acceleration = MoveSpeed; } else { if (nav_agent != null) { nav_agent.speed = 0; nav_agent.acceleration = 0; } } //check to see if we can attack the player if (can_attack && (target != null || !target.IsDead()) && Vector3.Distance(target.transform.position, transform.position) < AttackRange / 100.0f) { windup -= Time.deltaTime; } else { //reset windup windup = BaseAttackTime; } if (can_attack && windup <= 0) { //attack the enemy target target.Damage(Damage, this); //reset windup windup = BaseAttackTime; } }