転職を繰り返したサラリーマンの多趣味ブログ

30才未経験でSEに転職した人の多趣味ブログ

【Unity】GameObjectをキーボードで操作する方法③

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerManager : MonoBehaviour {

    // ブロックレイヤー
    public LayerMask blockLayer;

    private Rigidbody2D rbody;

    // 移動速度固定値
    private const float MOVE_SPEED = 3;

    // 移動速度
    private float moveSpeed;

    private float jumpPower = 400;

    private bool goJump = false;

    private bool canJump = false;

    private bool usingButtons = false;

    // 移動方向定義
    public enum MOVE_DIR
    {
        STOP,  // 0
        LEFT,  // 1
        RIGHT, // 2
    };

    private MOVE_DIR moveDirection = MOVE_DIR.STOP; // 初期値0

	// Use this for initialization
	void Start () {

        rbody = GetComponent<Rigidbody2D>();

	}

    private void Update()
    {
        canJump = 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);

        if(!usingButtons)
        {
            float x = Input.GetAxisRaw("Horizontal");

            if (x == 0)
            {
                moveDirection = MOVE_DIR.STOP;
            } else
            {
                if (x < 0)
                {
                    moveDirection = MOVE_DIR.LEFT;
                } else
                {
                    moveDirection = MOVE_DIR.RIGHT;
                }
            }

            if (Input.GetKeyDown("space"))
            {
                PushJumpButton();
            }
        }
    }

    // Update is called once per frame
    void FixedUpdate () {

        switch(moveDirection)
        {
            // 0の場合、停止
            case MOVE_DIR.STOP:
                moveSpeed = 0;
                break;
            // 1の場合、左に移動
            case MOVE_DIR.LEFT:
                moveSpeed = MOVE_SPEED * -1;
                transform.localScale = new Vector2(-1, 1);
                
                break;
            // 2の場合、右に移動
            case MOVE_DIR.RIGHT:
                moveSpeed = MOVE_SPEED;
                transform.localScale = new Vector2(1, 1);
                
                break;

        }


        rbody.velocity = new Vector2(moveSpeed, rbody.velocity.y);

        if(goJump)
        {
            rbody.AddForce(Vector2.up * jumpPower);
            goJump = false;
        }

	}

    public void PushLeftButton()
    {
        // moveDirectionを1に設定
        moveDirection = MOVE_DIR.LEFT;
        usingButtons = true;

    }

    public void PushRightButton()
    {
        // moveDirectionを2に設定
        moveDirection = MOVE_DIR.RIGHT;
        usingButtons = true;

    }

    public void ReleaseMoveButton()
    {
        // moveDirectionを0に設定
        moveDirection = MOVE_DIR.STOP;
        usingButtons = true;
    }

    public void PushJumpButton()
    {
        if(canJump)
        {
            goJump = true;
        }
    }
}

【スポンサーリンク】