private IEnumerator AttackRun(Destructible target) { is_action = true; bool is_ranged = target != null && CanAttackRanged(target); //Start animation if (onAttack != null) { onAttack.Invoke(target, is_ranged); } //Face target FaceTorward(target.transform.position); //Wait for windup float windup = GetAttackWindup(); yield return(new WaitForSeconds(windup)); attack_timer = 0f; //Ranged attack if (target != null && is_ranged) { ItemData equipped = GetEquippedItem(EquipSlot.Hand); ItemData projectile = GetFirstItemInGroup(equipped.projectile_group); if (projectile != null) { PlayerData.Get().RemoveItem(projectile.id, 1); Vector3 pos = GetProjectileSpawnPos(equipped); Vector3 dir = target.GetCenter() - pos; GameObject proj = Instantiate(projectile.projectile_prefab, pos, Quaternion.LookRotation(dir.normalized, Vector3.up)); proj.GetComponent <Projectile>().dir = dir.normalized; proj.GetComponent <Projectile>().damage = equipped.damage; } } //Melee attack else if (IsAttackTargetInRange()) { target.TakeDamage(GetAttackDamage()); if (onAttackHit != null) { onAttackHit.Invoke(target); } } //Wait for the end of the attack before character can move again float windout = GetAttackWindout(); yield return(new WaitForSeconds(windout)); is_action = false; }
//Detect if there is an obstacle in front of the animal private void DetectFronted() { float radius = destruct.hit_size * 2f; Vector3 center = destruct.GetCenter(); Vector3 dir = move_target_next - transform.position; Vector3 dirl = Quaternion.AngleAxis(-45f, Vector3.up) * dir.normalized; Vector3 dirr = Quaternion.AngleAxis(45f, Vector3.up) * dir.normalized; RaycastHit h, hl, hr; bool f1 = Physics.Raycast(center, dir.normalized, out h, radius); bool fl = Physics.Raycast(center, dirl.normalized, out hl, radius); bool fr = Physics.Raycast(center, dirr.normalized, out hr, radius); f1 = f1 && (target == null || h.collider.gameObject != target.gameObject); fl = fl && (target == null || hl.collider.gameObject != target.gameObject); fr = fr && (target == null || hr.collider.gameObject != target.gameObject); is_fronted = f1 || fl || fr; is_fronted_left = fl; is_fronted_right = fr; }