日々是好日~every day is a good day~

日常の中の非日常の備忘録

【Unity】動くブロック

ゲームに応用できそうな オブジェクトに簡単な動きをつけたものを色々とアップしてきましたが
今回は2Dアクションゲームとかで見かける動くブロックを作っていきます
1.3つのオブジェクトLeftObject RightObject CenterObjectをシーン内に作成
今回はUnityの無料アセット(Simple 2D Platformer Assets Pack)を使いました

2.プロジェクトタブ内で右クリック
作成/C#スクリプトを選択してLeftObjMove RightObjMove CenterObjMoveという3つのスクリプトを作成

3.LeftObjMoveを編集

using System.Collections;
using UnityEngine;

public class LeftObjMove : MonoBehaviour
{
    [SerializeField] float moveSpeed = 20f;
    private bool goDown;
    private bool isMove;
    // Start is called before the first frame update
    void Start()
    {
        goDown = true;
        isMove = true;
    }

    // Update is called once per frame
    void Update()
    {
        if (isMove)
        {
            isMove = false;
            if (goDown)
            {
                StartCoroutine(MoveDown());
            }
            else
            {
                StartCoroutine(MoveUp());
            }
        }
    }
    private IEnumerator MoveDown()
    {
        while (transform.rotation.z > -0.2f)
        {
            transform.Rotate(0, 0, -moveSpeed * Time.deltaTime);
            yield return null;
        }
        goDown = false;
        isMove = true;
    }
    private IEnumerator MoveUp()
    {
        while (transform.rotation.z < 0.2f)
        {
            transform.Rotate(0, 0, moveSpeed * Time.deltaTime);
            yield return null;
        }
        goDown = true;
        isMove = true;
    }
}

4.RightObjMoveを編集

using System.Collections;
using UnityEngine;

public class RightObjMove : MonoBehaviour
{

    [SerializeField] float moveSpeed = 1f;
    private Vector3 downPosition;
    private Vector3 startPosition;
    private bool goDown;
    private bool isMove;
    // Start is called before the first frame update
    void Start()
    {
        startPosition = transform.position;
        downPosition = new Vector3(startPosition.x, startPosition.y-2f, startPosition.z);
        goDown = true;
        isMove = true;
    }

    // Update is called once per frame
    void Update()
    {
        if (isMove)
        {
            isMove = false;
            if (goDown)
            {
                StartCoroutine(MoveDown());
            }
            else
            {
                StartCoroutine(MoveUp());
            }
        }
    }
    private IEnumerator MoveDown()
    {
        while (downPosition != transform.position)
        {
            transform.position = Vector3.MoveTowards(transform.position, downPosition, moveSpeed * Time.deltaTime);
            yield return null;
        }
        goDown = false;
        isMove = true;
    }
    private IEnumerator MoveUp()
    {
        while (startPosition != transform.position)
        {
            transform.position = Vector3.MoveTowards(transform.position, startPosition, moveSpeed * Time.deltaTime);
            yield return null;
        }
        goDown = true;
        isMove = true;
    }
}

5.CenterObjMoveを編集

using System.Collections;
using UnityEngine;

public class CenterObjMove : MonoBehaviour
{
    [SerializeField] float moveSpeed = 1f;
    private Vector3 rightPosition;
    private Vector3 leftPosition;
    private Vector3 startPosition;
    private bool goRight;
    private bool isMove;
    // Start is called before the first frame update
    void Start()
    {
        startPosition = transform.position;
        leftPosition = new Vector3(startPosition.x - 2f, startPosition.y, startPosition.z);
        rightPosition = new Vector3(startPosition.x + 2f, startPosition.y, startPosition.z);
        goRight = true;
        isMove = true;
    }

    // Update is called once per frame
    void Update()
    {
        if (isMove)
        {
            isMove = false;
            if (goRight)
            {
                StartCoroutine(MoveRight());
            }
            else
            {
                StartCoroutine(MoveLeft());
            }
        } 
    }
    private IEnumerator MoveRight()
    {
        while (rightPosition != transform.position)
        {
            transform.position = Vector3.MoveTowards(transform.position, rightPosition, moveSpeed * Time.deltaTime);
            yield return null;
        }
        goRight = false;
        isMove = true;
    }
    private IEnumerator MoveLeft()
    {
        while (leftPosition != transform.position)
        {
            transform.position = Vector3.MoveTowards(transform.position, leftPosition, moveSpeed * Time.deltaTime);
            yield return null;
        }
        goRight = true;
        isMove = true;
    }
}

6.スクリプトをそれぞれのオブジェクトにドラッグ&ドロップ

これで実行するとこんな感じの動きになります

どこかで見たことがある動きでしょ
無料アセットDOTweenを使ったらもっと簡単なプログラムになるでしょうが
今回はあえてコルーチンを使って動き方を意識してみました