始めに
フライトゲームやFPSでプレイヤーを動かすスクリプトを書きました。
プレイヤーの動かし方が分からない人は必見です!
(これはmacで開発することが前提となっています)
下準備
- unityのnewを選択
![スクリーンショット 2020-02-16 20.28.41.png]()
- Project nameを決めてCreate projectを押す
![スクリーンショット 2020-02-16 20.31.50.png]()
- Create -> 3D Object -> Planeをクリック、地面を作ります。
![スクリーンショット 2020-02-16 20.45.42.png]()
- Create -> 3D Object -> Cube、今回はこれを動かします。
![スクリーンショット 2020-02-16 20.46.41.png]()
- Cube -> Add Componentをクリック'Rigidbody'と検索Rigidbodyをクリックして追加する
![スクリーンショット 2020-02-16 20.54.18.png]()
- Main CameraをドラクアンドドロップでCubeに入れる
![スクリーンショット 2020-02-16 20.59.28.png]()
- ProjectのCreateからC#Scriptを選択してクリック
![スクリーンショット 2020-02-16 21.03.09.png]()
スクリプト
usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;publicclassPlayer_controller:MonoBehaviour{// Start is called before the first frame updatevoidStart(){}// Update is called once per framevoidUpdate(){if(Input.GetKey(KeyCode.UpArrow)){transform.position+=newVector3(0,0,0.1f);}if(Input.GetKey(KeyCode.DownArrow)){transform.position+=newVector3(0,0,-0.1f);}if(Input.GetKey(KeyCode.RightArrow)){transform.position+=newVector3(0.1f,0,0);}if(Input.GetKey(KeyCode.LeftArrow)){transform.position+=newVector3(-0.1f,0,0);}}}開いたプログラムの内容を全て削除して上のスクリプトをコピペしてコマンドSで保存。
unityに戻りこのスクリプトをCubeにドラクアンドドロップをする。
これで上の三角のボタンを押せば終了です。
もし、Cubeにスクリプトを入れられなかったらスクリプトを右クリック -> Remameを押して'Player_controller'と入れてみてください!
最後に
最後まで見てくださってありがとうございます。
Qiitaは始めたばかりなので間違っていたら遠慮なく指摘してください!よろしくお願いします。








