2Dシューティング_ ルームで敵を管理するコード
+ボタン押下でコードを確認できます!
エディタでアタッチし忘れている可能性が高いです。
- Enemy
- This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Enemy : Movement { //このスクリプトの役目 //追跡や攻撃などのAI部分 //public bool Test;//後で消す private Transform player; private Vector3 playerLastPos, startPos, movementPos; [SerializeField] private float chaseSpeed = 0.8f, turningDelay = 1f; private float lastFollowTime, turningTimeDelay = 1f; private Vector3 tempScalse; private bool attacked; [SerializeField] private float damageCooldown = 1f; private float damageCooldownTimer; [SerializeField] private int damageAmount = 1; private Health enemyHealth; private Animator animator; private void Start() { player = GameObject.FindWithTag("Player").transform; playerLastPos = player.position; startPos = transform.position; lastFollowTime = Time.time; turningTimeDelay *= turningDelay; enemyHealth = GetComponent<Health>(); animator = GetComponent<Animator>(); } private void FixedUpdate() { if (!enemyHealth.IsAllive() || !player) { return; } MoveAnimation(); TurnAround(); ChaseingPlayer(); } void ChaseingPlayer() { if (HasPlayerTarget) { if (!attacked) { //関数を呼ぶ Chase(); } else { if (Time.time < damageCooldownTimer) { movementPos = startPos - transform.position; } else { attacked = false; } } } else { movementPos = startPos - transform.position; if (Vector3.Distance(transform.position, startPos) < 0.1f) { movementPos = Vector3.zero; } } CharacterMovement(movementPos.x, movementPos.y); } void Chase() { if (Time.time - lastFollowTime > turningTimeDelay) { playerLastPos = player.transform.position; lastFollowTime = Time.time; } if (Vector3.Distance(transform.position, playerLastPos) > 0.15f) { movementPos = (playerLastPos - transform.position).normalized * chaseSpeed; } else { movementPos = Vector3.zero; } } void TurnAround() { tempScalse = transform.localScale; if (HasPlayerTarget) { if (player.position.x > transform.position.x) { tempScalse.x = Mathf.Abs(tempScalse.x); } if (player.position.x < transform.position.x) { tempScalse.x = -Mathf.Abs(tempScalse.x); } } else { if (startPos.x > transform.position.x) { tempScalse.x = Mathf.Abs(tempScalse.x); } if (startPos.x < transform.position.x) { tempScalse.x = -Mathf.Abs(tempScalse.x); } } transform.localScale = tempScalse; } private void OnTriggerEnter2D(Collider2D collision) { if (collision.CompareTag("Player")) { damageCooldownTimer = Time.time + damageCooldown; attacked = true; collision.GetComponent<Health>().TakeDamage(damageAmount); } } void MoveAnimation() { if (GetMoveDelta().sqrMagnitude > 0) { animator.SetBool("Walk", true); } else { animator.SetBool("Walk", false); } } }
- Movement
- This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Movement : MonoBehaviour { //このスクリプトはBoss以外のキャラ移動を扱う(PlayerもEnemyも) [SerializeField] protected float xSpeed = 1.5f, ySpeed = 1.5f; private Vector2 moveDelta; private bool _hasPlayerTarget; public bool HasPlayerTarget { get { return _hasPlayerTarget; } set { _hasPlayerTarget = value; } } protected void CharacterMovement(float x, float y) { moveDelta = new Vector2(x * xSpeed, y * ySpeed); transform.Translate(moveDelta.x * Time.deltaTime, moveDelta.y * Time.deltaTime, 0); } public Vector2 GetMoveDelta() { return moveDelta; } }
- EnemyTarget
- This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
using System.Collections; using System.Collections.Generic; using UnityEngine; public enum EnemyTarget { EnableTarget, DisableTarget } public class EnemyRoom : MonoBehaviour { //プレイヤーの入退場を検知する [SerializeField] private EnemyTarget enemyTarget; [SerializeField] private EnemyRoomManager enemyRoomManager; private void OnTriggerEnter2D(Collider2D collision) { if (collision.CompareTag("Player")) { if (enemyTarget == EnemyTarget.EnableTarget) { enemyRoomManager.EnablePlayerTarget(); } else { enemyRoomManager.DisablePlayerTarget(); } } } }
- EnemyRoomManager
- This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
using System.Collections; using System.Collections.Generic; using UnityEngine; public class EnemyRoomManager : MonoBehaviour { //このスクリプトの役目 //部屋のエネミーを管理 [SerializeField] private List<Movement> enemies; //関数作成(リストからエネミーを削除、リストの中身確認して0ならドア解除関数) private void Start() { foreach (Transform tr in GetComponentInChildren<Transform>()) { enemies.Add(tr.GetComponent<Movement>()); } } //ターゲットの判定を切り替える関数作成(2つ) public void EnablePlayerTarget() { foreach (Movement move in enemies) { move.HasPlayerTarget = true; } } public void DisablePlayerTarget() { foreach (Movement move in enemies) { move.HasPlayerTarget = false; } } }