unityでとりあえずプレイヤーを動かすスクリプト
始めに
フライトゲームやFPSでプレイヤーを動かすスクリプトを書きました。
プレイヤーの動かし方が分からない人は必見です!
(これはmacで開発することが前提となっています)
下準備
- unityのnewを選択
- Project nameを決めてCreate projectを押す
- Create -> 3D Object -> Planeをクリック、地面を作ります。
- Create -> 3D Object -> Cube、今回はこれを動かします。
- Cube -> Add Componentをクリック'Rigidbody'と検索Rigidbodyをクリックして追加する
- Main CameraをドラクアンドドロップでCubeに入れる
- ProjectのCreateからC#Scriptを選択してクリック
- デリートキーを押し、'Player_controller'と入力
- 作成したスクリプトをダブルクリックで開く
これで下準備は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);
}
}
}
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は始めたばかりなので間違っていたら遠慮なく指摘してください!よろしくお願いします。
Author And Source
この問題について(unityでとりあえずプレイヤーを動かすスクリプト), 我々は、より多くの情報をここで見つけました https://qiita.com/lot-uni/items/06b3d449087883a4f854著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .