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

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

初心者のためのUnity講座! とっても簡単に効果音を鳴らす方法の巻 サイドビューアクションゲーム編

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

前回

simanezumi1989.hatenablog.com

今回

BGMやSEを鳴らす方法について紹介します。

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

使用した音源

BGM:https://maoudamashii.jokersounds.com/list/bgm11.html
アイテム取得/ジャンプ/ゲームクリア/ゲームオーバー:https://taira-komori.jpn.org/game01.html
敵撃破:https://taira-komori.jpn.org/attack01.html

コード

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;
    Animator animator;
    bool isDead;


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


    void Start()
    {
        rigidbody2D = GetComponent<Rigidbody2D>();
        animator = GetComponent<Animator>();
        isDead = false;
    }

    void Update()
    {
        if (isDead)
        {
            return;
        }
        float x = Input.GetAxis("Horizontal");
        animator.SetFloat("speed", Mathf.Abs(x));
        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())
        {
            if (Input.GetKeyDown("space"))
            {
                Jump();
                animator.SetBool("isJumping", true);
            }
            else
            {
                animator.SetBool("isJumping", false);
            }
        }
    }
    private void FixedUpdate()
    {
        if (isDead)
        {
            return;
        }

        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 (isDead)
        {
            return;
        }

        if (collision.gameObject.tag == "Trap")
        {
            PlayerDeath();
        }
        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
            {
                // 正面からぶつかったら
                PlayerDeath();
            }
        }
    }
    void PlayerDeath()
    {
        isDead = true;
        animator.Play("PlayerDeathAnimation");
        rigidbody2D.velocity = new Vector2(0, 0);
        Jump();
        BoxCollider2D boxCollider2D = GetComponent<BoxCollider2D>();
        CircleCollider2D circleCollider2D = GetComponent<CircleCollider2D>();
        Destroy(boxCollider2D);
        Destroy(circleCollider2D);
        gameManager.GameOver();
    }
}

とりあえずサイドビューアクションはこれにて終了です。お疲れ様でした^^ ただ今後は、Unityの拡張機能がうまく動かない人向けの修正動画を紹介していきます。

スタジオしまづから

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