Unityゲーム開発スタジオ スタジオしまづ

初心者向けUnity講座・ 統計・機械学習関係についてつらつらと

初心者のためのUnity講座! 敵を踏んだら攻撃できたけど正面突破だとやられますの巻 サイドビューアクションゲーム編

みなさんこんにちはスタジオしまづの嶋津です。

前回

敵を崖に落とさない実装ができました。 simanezumi1989.hatenablog.com

今回

敵への攻撃と敵からの攻撃を紹介します。

youtu.be (役に立ったという方はチャンネル登録していただけると嬉しいです^^)

コードは以下のようになります。

using UnityEngine;

public class EnemyManager : MonoBehaviour
{
    // [SerializeField] GameManager gameManager;
    [SerializeField] LayerMask blockLayer;
    Rigidbody2D rigidbody2D;
    float speed = 0;

    public enum MOVE_DIRECTION
    {
        STOP,
        LEFT,
        RIGHT,
    }
    MOVE_DIRECTION moveDirection = MOVE_DIRECTION.RIGHT;

    void Start()
    {
        rigidbody2D = GetComponent<Rigidbody2D>();
    }

    void Update()
    {
        if (!IsGround())
        {
            // 向きを変える
            ChangeDirection();
        }
    }
    private void FixedUpdate()
    {
        switch (moveDirection)
        {
            case MOVE_DIRECTION.STOP:
                speed = 0;
                break;
            case MOVE_DIRECTION.LEFT:
                transform.localScale = new Vector3(-1, 1, 1);
                speed = -3;
                break;
            case MOVE_DIRECTION.RIGHT:
                transform.localScale = new Vector3(1, 1, 1);
                speed = 3;
                break;
        }
        rigidbody2D.velocity = new Vector2(speed, rigidbody2D.velocity.y);
    }
    bool IsGround()
    {
        Vector3 startVec = transform.position + transform.right * 0.5f* transform.localScale.x;
        Vector3 endVec = startVec - transform.up * 0.5f;
        Debug.DrawLine(startVec, endVec);
        return Physics2D.Linecast(startVec, endVec, blockLayer);    
    }

    void ChangeDirection()
    {
        if (moveDirection == MOVE_DIRECTION.RIGHT)
        {
            // 左に移動
            moveDirection = MOVE_DIRECTION.LEFT;
       }
        else
        {
            // 右に移動
            moveDirection = MOVE_DIRECTION.RIGHT;
        }
    }
    public void DestroyEnemy()
    {
        Destroy(this.gameObject);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    [SerializeField] GameManager gameManager;
    [SerializeField] LayerMask blockLayer;
    Rigidbody2D rigidbody2D;
    float speed = 0;

    // Jump
    float jumpPower = 400;

    public enum MOVE_DIRECTION
    {
        STOP,
        LEFT,
        RIGHT,
    }
    MOVE_DIRECTION moveDirection = MOVE_DIRECTION.STOP;

    void Start()
    {
        rigidbody2D = GetComponent<Rigidbody2D>();
    }

    void Update()
    {
        float x = Input.GetAxis("Horizontal");
        if (x==0)
        {
            // 止まる
            moveDirection = MOVE_DIRECTION.STOP;
        }
        else if (x>0)
        {
            // 右に移動
            moveDirection = MOVE_DIRECTION.RIGHT;
        }
        else if (x < 0)
        {
            // 左に移動
            moveDirection = MOVE_DIRECTION.LEFT;
        }
        if (IsGround() && Input.GetKeyDown("space"))
        {
            Jump();
        }

    }
    private void FixedUpdate()
    {
        switch (moveDirection)
        {
            case MOVE_DIRECTION.STOP:
                speed=0;
                break;
            case MOVE_DIRECTION.LEFT:
                transform.localScale = new Vector3(-1,1,1);
                speed= -3;
                break;
            case MOVE_DIRECTION.RIGHT:
                transform.localScale = new Vector3(1, 1, 1);
                speed = 3;
                break;
        }
        rigidbody2D.velocity = new Vector2(speed, rigidbody2D.velocity.y);
    }

    void Jump()
    {
        rigidbody2D.AddForce(Vector2.up * jumpPower);
    }
    bool IsGround()
    {
        return Physics2D.Linecast(transform.position-transform.right*0.3f, transform.position - transform.up * 0.1f, blockLayer)
             || Physics2D.Linecast(transform.position + transform.right * 0.3f, transform.position - transform.up * 0.1f, blockLayer);
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.tag == "Trap")
        {
            gameManager.GameOver();
        }
        if (collision.gameObject.tag == "Finish")
        {
            gameManager.GameClear();
        }
        if (collision.gameObject.tag == "Item")
        {
            collision.gameObject.GetComponent<ItemManager>().GetItem();
        }
        if (collision.gameObject.tag == "Enemy")
        {
            EnemyManager enemy = collision.gameObject.GetComponent<EnemyManager>();
            if(this.transform.position.y > enemy.transform.position.y)
            {
                // 踏んだら
                // プレイヤーをジャンプさせる
                rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, 0);
                Jump();
                enemy.DestroyEnemy();
            }
            else
            {
                // 正面からぶつかったら
                Destroy(this.gameObject);
                gameManager.GameOver();
            }
        }
    }
}

次の動画ではトラップの実装を紹介します。

スタジオしまづから

ゲームをリリースしたい人向けのUnity講座を動画販売しています。c#の基礎からリリースまで体系的に学びたい方はぜひ。
https://www.udemy.com/studio_shimazu_nfrpg/?couponCode=FREE_FIRST
  また、一人でゲームを作るのは難しいという方はゲームリリースを目的とするオンラインサロンもやっているのでよければどうぞ!話だけでもという方はTwitterのDMだけでもOKです^^ camp-fire.jp