1
0

アクションゲームっぽいものを作る

Posted at

PlayerController

C#
説明

2Dのプレイヤー移動処理(左右の反転には対応していない)
移動範囲の設定(Clamp)
スペースキーで画面内の敵キャラに攻撃できる
Weapon(武器)を取得して当たり判定のオンオフを行っている
プレイヤーにダメージを与える関数
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    private bool isDead = false;

    [SerializeField] float playerMoveSpeed;
    [SerializeField] float minX = -7.0f;
    [SerializeField] float maxX = 7.0f;
    [SerializeField] float minY = -4.0f;
    [SerializeField] float maxY = 4.0f;

    // ライフ
    [SerializeField] int Life;

    // プレイヤー
    private GameObject currentPlayer;

    // 武器
    private GameObject weapon;

    private void Start()
    {
        isDead = false;

        // コライダー取得
        weapon = GameObject.Find("weapon");

        currentPlayer = this.gameObject;
    }

    private void Update()
    {
        HandlePlayerInput();

        if (isDead)
        {
            currentPlayer.SetActive(false);
        }

        if (Input.GetKeyDown(KeyCode.Space))
        {
            StartCoroutine(Attack(1f));
        }
    }

    private void HandlePlayerInput()
    {
        float horizontalInput = Input.GetAxis("Horizontal");
        float verticalInput = Input.GetAxis("Vertical");
        Vector3 movement = new Vector3(horizontalInput, verticalInput, 0) * playerMoveSpeed * Time.deltaTime;
        Vector3 newPosition = currentPlayer.transform.position + movement;

        // 移動可能な範囲を制限
        newPosition.x = Mathf.Clamp(newPosition.x, minX, maxX);
        newPosition.y = Mathf.Clamp(newPosition.y, minY, maxY);

        currentPlayer.transform.position = newPosition;
    }

    public void AddPlayerDamage(int damage)  // 敵スクリプトで呼ぶ
    {
        if(Life >= 1)
            Life -= damage;

        Debug.Log("Player"+ "Now Hp" + Life);
        if (Life <= 0)
            isDead = true;
    }

    private IEnumerator Attack(float duration)
    {
        weapon.GetComponent<Weapon>().IsValid(true);

        yield return new WaitForSeconds(duration);

        weapon.GetComponent<Weapon>().IsValid(false);  
    }
}

EnemyController

C#

敵にダメージを与える関数
プレイヤーの武器と当たった時のOnCollisionEnter2Dで自身にダメージを与える関数を呼ぶ
プレイヤーと当たった時プレイヤー側にダメージを与える関数を呼ぶ
using UnityEngine;

public class EnemyController : MonoBehaviour
{
    // 死亡フラグ
    private bool isDead = false;

    // ライフ
    [SerializeField] int Life;

    // 攻撃力
    [SerializeField] int Attack;

    // 敵
    private GameObject currentEnemy;

    // プレイヤー
    public GameObject playerObject;

    private void Start()
    {
        isDead = false;
        currentEnemy = this.gameObject;
    }

    void Update()
    {
        if (isDead)
        {
            currentEnemy.SetActive(false);
        }
    }

    private void AddEnemyDamage(int damage)
    {
        if (Life >= 1)
            Life -= damage;

        Debug.Log("Enemy" + "Now Hp" + Life);
        if (Life <= 0)
            isDead = true;
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if(collision.gameObject.tag == "Weapon")
        {
            Debug.Log("Weapon");
            Weapon weapon = collision.gameObject.GetComponent<Weapon>();
            if (weapon == null) return;
            Debug.Log($"EnemyHP +{weapon.Power}");
            AddEnemyDamage(weapon.Power);
        }
        if(collision.gameObject.tag == "Player")
        {
            PlayerController playerController 
                = collision.gameObject.GetComponent<PlayerController>();
            if (playerController == null) return;
            Debug.Log($"PlayerHP +{Attack}");
            playerController.AddPlayerDamage(Attack);
        }
    }
}

Weapon

C#

プレイヤーの武器につけるスクリプト
自身の当たり判定と武器の威力を持っている
コライダーを指定の時間だけ有効にする(3秒)
using UnityEngine;

public class Weapon : MonoBehaviour
{
    private PolygonCollider2D polygonCollider;
    private bool attackflg = false;

    private Coroutine coroutine;

    [SerializeField] int power;

    public int Power { get => power; }

    void Start()
    {
        polygonCollider = GetComponent<PolygonCollider2D>();

        attackflg = false;
    }

    void Update()
    {
        if (attackflg)
        {
            // コライダーがオンになった時の処理
            if (!polygonCollider.enabled)
            {
                // コルーチンを開始
                coroutine = StartCoroutine(EnableColliderForDuration(3.0f));
            }
        }
    }

    public void IsValid(bool flg)  // プレイヤー側で呼ぶ
    {
        Debug.Log(flg);
        attackflg = flg;
    }

    // コライダーを指定の時間だけ有効にするコルーチン
    private IEnumerator EnableColliderForDuration(float duration)
    {
        polygonCollider.enabled = true; // コライダーを有効にする

        yield return new WaitForSeconds(duration); // 指定の時間待機

        polygonCollider.enabled = false; // コライダーを無効にする

        attackflg = false;
    }
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0