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

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

初心者のためのUnity講座! キャラクターが壁に引っかかるバグと敵キャラへの攻撃のバグ修正の巻 サイドビューアクションゲーム編

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

前回

simanezumi1989.hatenablog.com

今回

キャラクターが壁に引っかかるバグと敵キャラを攻撃したのに攻撃された判定になるバグの修正方法について紹介します。

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

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()
    {
        Debug.DrawLine(transform.position - transform.right * 0.2f, transform.position - transform.up * 0.1f);
        Debug.DrawLine(transform.position + transform.right * 0.2f, transform.position - transform.up * 0.1f);
        return Physics2D.Linecast(transform.position-transform.right*0.2f, transform.position - transform.up * 0.1f, blockLayer)
             || Physics2D.Linecast(transform.position + transform.right * 0.2f, 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+0.2f > enemy.transform.position.y)
            {
                // 踏んだら
                // プレイヤーをジャンプさせる
                rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, 0);
                Jump();
                enemy.DestroyEnemy();
            }
            else
            {
                // 正面からぶつかったら
                Destroy(this.gameObject);
                gameManager.GameOver();
            }
        }
    }
}

次の動画ではキャラクターのアニメーション切り替え(Run/Jump)を紹介します。

スタジオしまづから

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