Unityをスクリプトで動かしたい!

Unityでオブジェクトを操作する方法を紹介してます!

Unityスクリプトでオブジェクトを移動させる(動画あり)

Unityでのオブジェクトの移動方法を紹介します。

~
public class Move : MonoBehaviour
{
    [SerializeField]
    float Speed;    // 進むスピード
    void Update()
    {
        //右の矢印キーが押された場合
        if(Input.GetKey(KeyCode.RightArrow))
        {
            //x軸のプラス方向にSpeedの値を加算
            this.transform.position += new Vector3(Speed,0,0);
        }
        //左の矢印キーが押された場合
        if(Input.GetKey(KeyCode.LeftArrow))
        {
            //x軸のマイナス方向にSpeedの値を加算
            this.transform.position += new Vector3(-Speed,0,0);
        }
        //y軸のプラス方向にSpeedの値を加算
        if (Input.GetKey(KeyCode.UpArrow))
        {
            this.transform.position += new Vector3(0, Speed, 0);
        }
        //y軸のマイナス方向にSpeedの値を加算
        if (Input.GetKey(KeyCode.DownArrow))
        {
            this.transform.position += new Vector3(0, -Speed, 0);
        }
    }
}
~

f:id:SugieSan:20210218222308g:plain
キーボードのアローキーが押された場合その方向に移動するようにしました。
押されたキーが分かりやすい様に赤く表示されるようにしました。