norablog-techのブログ

このブログは、自分の忘備録的にアウトプットするためのブログとなっております。

【Unity】プレイヤー機を動かす(プログラム編その1)

今回は”プレーヤー機”をキーボードで動かすプログラムを作成します。
"Input.GetKey"と"Input.GetAxis"の両方を使ってプログラムを書いてみます。
最終的にプレイヤー機の移動には、"Input.GetAxis"を使います。
"Input.GetKey"は移動以外にも、何か動作をさせるのに使えます。
後に弾を発射するのに使う予定です。

 

This time, I will create a program to move the "player machine" with the keyboard.
Let's write a program using both "Input.GetKey" and "Input.GetAxis".
Finally, use "Input.GetAxis" to move the player machine.
"Input.GetKey" can be used to do something other than move.
I plan to use it to fire bullets later.

☆ Information is also disclosed on my blog.
https://norablog-tech.hatenablog.com/

 


www.youtube.com

 

【Unityを使った”2Dシューティングゲームの作成(プログラム編その1)】

今回は”プレーヤー機”をキーボードで動かすプログラムを作成します。
プレイヤー機をプログラムで動かすためには、キー入力とプレーヤー機を結び付けてやる必要があります。
代表的と思われる2つのプログラムを紹介します。

This time, I will create a program to move the "player machine" with the keyboard.
In order to operate the player machine programmatically, it is necessary to connect the key input and the player machine.
Here are two programs that seem to be representative.

 

0.インスペクターでプレイヤー機にスクリプトを”追加”します。
  今回は"DiamondController"をいう名前をつけました。
 今後何かをコントロールする場合は"--Controller"という名称に統一します。

0. "Add" the script to the player machine in the Inspector.
   This time, I named it "Diamond Controller".
 When controlling something in the future, the name will be unified to "--Controller".

 

1."Input.GetKey"を使用する。
 個々のキーに動作を割り当てる。
 移動以外にもいろいろな動作にキーを割り当てることができる。
※Unityでは移動の指定は省略していても、常に"f(フロート)"です。
※移動量は各自調整してください。

1. Use "Input.GetKey".
 Assign actions to individual keys.
 Keys can be assigned to various actions other than movement.
* In Unity, even if the move specification is omitted, it is always "f (float)".
* Please adjust the amount of movement by yourself.

 

コード(code):

using UnityEngine;
using System.Collections;

public class DiamondController : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    //"transform.Translate":ローカル座標(現在位置からの”相対的”位置の増減)
    //"Time.deltaTime":現在のフレームと前のフレームの間の時間を提供。
    //GameObjectを1秒あたりの単位でその方向に移動します。
    //"transform.position.x(y)":現在のx,y座標
    //"Vector3":3D ベクトルと位置の表現
    //Unity全体で3Dの位置と方向を渡すために使用。画面からはみ出さないように制御。

    // "transform.Translate": Local coordinates (increase / decrease "relative" position from current position)
    // "Time.deltaTime": Provides the time between the current frame and the previous frame.
    // Move the GameObject in that direction in units of seconds.
    // "transform.position.x (y)": current x, y coordinates
    // "Vector3": 3D vector and position representation
    // Used to pass 3D position and orientation throughout Unity. Control so that it does not protrude from the screen.

    void Update()
    {
        //矢印キーにX軸,Y軸方向の移動を割り当てる。Z軸方向は常に"0"
        if (Input.GetKey(KeyCode.LeftArrow))
        {
            transform.Translate(-2.0f * Time.deltaTime, 0, 0);
            if (transform.position.x < -8.5f)
            {
                transform.position = new Vector3(-8.5f, transform.position.y, 0);
            }
        }
        if (Input.GetKey(KeyCode.RightArrow))
        {
            transform.Translate(2.0f * Time.deltaTime, 0, 0);
            if (transform.position.x > 8.5f)
            {
                transform.position = new Vector3(8.5f, transform.position.y, 0);
            }
        }
        if (Input.GetKey(KeyCode.UpArrow))
        {
            transform.Translate(0, 2.0f * Time.deltaTime, 0);
            if (transform.position.y > 4.5f)
            {
                transform.position = new Vector3(transform.position.x, 4.5f, 0);
            }
        }
        if (Input.GetKey(KeyCode.DownArrow))
        {
            transform.Translate(0, -2.0f * Time.deltaTime, 0);
            if (transform.position.y < -4.5f)
            {
                transform.position = new Vector3(transform.position.x, -4.5f, 0);
            }
        }
    }
}

 

2."Input.GetAxis"を使う。
 ジョイスティック、ゲームパッド、キーボード、マウスを利用できる。
※Unityでは移動の指定は省略していても、常に"f(フロート)"です。
※移動量は各自調整してください。

2. Use "Input.GetAxis".
You can use the joystick, game pad, keyboard, and mouse.
* In Unity, even if the move specification is omitted, it is always "f (float)".
* Please adjust the amount of movement by yourself.

 

コード(code):

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

public class PlayerScript : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    //"Input.GetAxis":Horizontal と Vertical: w、a、s、d と矢印キー
    //"Time.deltaTime":現在のフレームと前のフレームの間の時間を提供。
    //GameObjectを1秒あたりの単位でその方向に移動します。
    //"Mathf.Clamp":与えられた最小 float 値と最大 float 値の範囲に値を制限します。
    //"transform.position":3D world space.(ワールド座標:画面内の絶対的位置)

 // "Input.GetAxis": Horizontal and Vertical: w, a, s, d and arrow keys
 // "Time.deltaTime": Provides the time between the current frame and the previous frame.
 // Move the GameObject in that direction in units of seconds.
 // "Mathf.Clamp": Limits the value to the range of given minimum and maximum float values.
 // "transform.position": 3D world space. (World coordinates: absolute position in the screen)

    void Update()
    {
        float dx = Input.GetAxis("Horizontal") * Time.deltaTime * 8f;
        float dy = Input.GetAxis("Vertical") * Time.deltaTime * 8f;

        transform.position = new Vector3(
                Mathf.Clamp(transform.position.x + dx, -9f, 9f),
                Mathf.Clamp(transform.position.y + dy, -5f, 5f),
                0f
            );
    }
}

3.各クラス、メソッドに関しては、Unityのスクリプトリファレンスを参照してください。(これ重要です)

3. For each class and method, refer to Unity script reference. (This is important)


4."Input.GetAxis"を使う方が、明らかにコードが短く、シンプルなので、こちらを採用。

4. Using "Input.GetAxis" is obviously shorter and simpler, so I chose this one.

 

ではまた。

see you.