LoginSignup
0
0

More than 3 years have passed since last update.

unityでとりあえずプレイヤーを動かすスクリプト

Posted at

始めに

フライトゲームやFPSでプレイヤーを動かすスクリプトを書きました。
プレイヤーの動かし方が分からない人は必見です!
(これはmacで開発することが前提となっています)

下準備

  1. unityのnewを選択 スクリーンショット 2020-02-16 20.28.41.png
  2. Project nameを決めてCreate projectを押すスクリーンショット 2020-02-16 20.31.50.png
  3. Create -> 3D Object -> Planeをクリック、地面を作ります。 スクリーンショット 2020-02-16 20.45.42.png
  4. Create -> 3D Object -> Cube、今回はこれを動かします。 スクリーンショット 2020-02-16 20.46.41.png
  5. Cube -> Add Componentをクリック'Rigidbody'と検索Rigidbodyをクリックして追加する スクリーンショット 2020-02-16 20.54.18.png
  6. Main CameraをドラクアンドドロップでCubeに入れる
    スクリーンショット 2020-02-16 20.59.28.png
  7. ProjectのCreateからC#Scriptを選択してクリック
    スクリーンショット 2020-02-16 21.03.09.png
  8. デリートキーを押し、'Player_controller'と入力
    スクリーンショット 2020-02-16 21.05.14.png
  9. 作成したスクリプトをダブルクリックで開く スクリーンショット 2020-02-16 21.13.27.png これで下準備はOKです。

スクリプト

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

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

    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey(KeyCode.UpArrow))
        {
            transform.position += new Vector3(0,0,0.1f);
        }
        if (Input.GetKey(KeyCode.DownArrow))
        {
            transform.position += new Vector3(0,0,-0.1f);
        }
        if (Input.GetKey(KeyCode.RightArrow))
        {
            transform.position += new Vector3(0.1f,0,0);
        }
        if (Input.GetKey(KeyCode.LeftArrow))
        {
            transform.position += new Vector3(-0.1f,0,0);
        }
    }

}

開いたプログラムの内容を全て削除して上のスクリプトをコピペしてコマンドSで保存。unityに戻りこのスクリプトをCubeにドラクアンドドロップをする。
これで上の三角のボタンを押せば終了です。
もし、Cubeにスクリプトを入れられなかったらスクリプトを右クリック -> Remameを押して'Player_controller'と入れてみてください!

最後に

最後まで見てくださってありがとうございます。
Qiitaは始めたばかりなので間違っていたら遠慮なく指摘してください!よろしくお願いします。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0