プログラミング逆引き辞典

~ 多言語対応のプログラミングレシピ ~

Unity3D オブジェクトの動かし方

キー操作でオブジェクトを動かす方法
 
 

■オブジェクトを用意する

・動かす対象:Cubeオブジェクト
・地面:Planeオブジェクト
 


 
 


■スクリプトを用意し、Cubeオブジェクトにアタッチ

・スクリプト:Sample.cs

 
 


■スクリプトを記述

・スクリプト:Sample.cs

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

public class Sample : MonoBehaviour
{
    //移動速度
    float move = 0.01f;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        //上を押している間
        if (Input.GetKey(KeyCode.UpArrow)) {
            //奥に移動
            gameObject.transform.Translate(new Vector3(0, 0, move));
        }
        //下を押している間
        if (Input.GetKey(KeyCode.DownArrow)) {
            //手前に移動
            gameObject.transform.Translate(new Vector3(0, 0, -1 * move));
        }
        //右を押している間
        if (Input.GetKey(KeyCode.RightArrow)) {
            //右に移動
            gameObject.transform.Translate(new Vector3(move, 0, 0));
        }
        //左を押している間
        if (Input.GetKey(KeyCode.LeftArrow)) {
            //左に移動
            gameObject.transform.Translate(new Vector3(-1 * move, 0, 0));
        }
    }
}

 
 


■transformプロパティを使用した移動

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

public class Sample : MonoBehaviour
{
    //移動速度
    float move = 0.01f;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        //上を押している間
        if (Input.GetKey(KeyCode.UpArrow)) {
            //奥に移動
            //gameObject.transform.Translate(new Vector3(0, 0, move));

            //transformプロパティを使用した場合
            gameObject.transform.Translate(transform.forward * move);
        }
        //下を押している間
        if (Input.GetKey(KeyCode.DownArrow)) {
            //手前に移動
            //gameObject.transform.Translate(new Vector3(0, 0, -1 * move));

            //transformプロパティを使用した場合
            gameObject.transform.Translate(transform.forward * -1 * move);
        }
        //右を押している間
        if (Input.GetKey(KeyCode.RightArrow)) {
            //右に移動

            //transformプロパティを使用した場合
            gameObject.transform.Translate(transform.right * move);
        }
        //左を押している間
        if (Input.GetKey(KeyCode.LeftArrow)) {
            //左に移動
            //gameObject.transform.Translate(new Vector3(-1 * move, 0, 0));

            //transformプロパティを使用した場合
            gameObject.transform.Translate(transform.right * -1 *  move);

        }
    }
}

 
 


■positionを使用した移動

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

public class Sample : MonoBehaviour
{
    //移動速度
    float move = 0.01f;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        //上を押している間
        if (Input.GetKey(KeyCode.UpArrow)) {
            //Cubeオブジェクトの現在位置を取得
            Vector3 pos = transform.position;

            //奥にmove分だけ移動
            pos.z += move;
            Vector3 newPos = new Vector3(pos.x, pos.y, pos.z);
            transform.position = newPos;
        }
        //下を押している間
        if (Input.GetKey(KeyCode.DownArrow)) {
            //Cubeオブジェクトの現在位置を取得
            Vector3 pos = transform.position;

            //奥にmove分だけ減算⇒手前にmove分だけ移動
            pos.z -= move;
            Vector3 newPos = new Vector3(pos.x, pos.y, pos.z);
            transform.position = newPos;
        }
        //右を押している間
        if (Input.GetKey(KeyCode.RightArrow)) {
            //Cubeオブジェクトの現在位置を取得
            Vector3 pos = transform.position;

            //右にmove分だけ移動
            pos.x += move;
            Vector3 newPos = new Vector3(pos.x, pos.y, pos.z);
            transform.position = newPos;
        }
        //左を押している間
        if (Input.GetKey(KeyCode.LeftArrow)) {
            //Cubeオブジェクトの現在位置を取得
            Vector3 pos = transform.position;

            //右にmove分だけ減算⇒左にmove分だけ移動
            pos.x -= move;
            Vector3 newPos = new Vector3(pos.x, pos.y, pos.z);
            transform.position = newPos;
        }
    }
}

 
 


■重力を使用した移動

CubeオブジェクトにRigidbodyをアタッチ

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

public class Sample : MonoBehaviour
{
    //Cubeオブジェクトにかける力量
    float force = 1f;

    //重力コンポーネント
    Rigidbody rd;

    // Start is called before the first frame update
    void Start()
    {
        //重力コンポーネントのインスタンス
        rd = GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void Update()
    {
        //上を押している間
        if (Input.GetKey(KeyCode.UpArrow)) {
            //奥にforce分だけ力をかける
            rd.AddForce(0, 0, force);
        }
        //下を押している間
        if (Input.GetKey(KeyCode.DownArrow)) {
            //手前にforce分だけ力をかける
            rd.AddForce(0, 0, -1 * force);
        }
        //右を押している間
        if (Input.GetKey(KeyCode.RightArrow)) {
            //右にforce分だけ力をかける
            rd.AddForce(force, 0, 0);
        }
        //左を押している間
        if (Input.GetKey(KeyCode.LeftArrow)) {
            //左にforce分だけ力をかける
            rd.AddForce(-1 * force, 0, 0);
        }
    }
}

 
 


■重力を使用した移動(オブジェクト回転無し)

※動きをスムーズにする為にCubeオブジェクトにかける力量を1.5fに変更
 
下記を使う

rd.constraints = RigidbodyConstraints.FreezeRotation;

 

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

public class Sample : MonoBehaviour
{
    //Cubeオブジェクトにかける力量
    float force = 1.5f;

    //重力コンポーネント
    Rigidbody rd;

    // Start is called before the first frame update
    void Start()
    {
        //重力コンポーネントのインスタンス
        rd = GetComponent<Rigidbody>();

        //Cubeオブジェクトの回転を無効
        rd.constraints = RigidbodyConstraints.FreezeRotation;
    }

    // Update is called once per frame
    void Update()
    {
        //上を押している間
        if (Input.GetKey(KeyCode.UpArrow)) {
            //奥にforce分だけ力をかける
            rd.AddForce(0, 0, force);
        }
        //下を押している間
        if (Input.GetKey(KeyCode.DownArrow)) {
            //手前にforce分だけ力をかける
            rd.AddForce(0, 0, -1 * force);
        }
        //右を押している間
        if (Input.GetKey(KeyCode.RightArrow)) {
            //右にforce分だけ力をかける
            rd.AddForce(force, 0, 0);
        }
        //左を押している間
        if (Input.GetKey(KeyCode.LeftArrow)) {
            //左にforce分だけ力をかける
            rd.AddForce(-1 * force, 0, 0);
        }
    }
}