Unityで3Dオブジェクトの色んな移動方法が確認できるシステム【Unityスクール】

Unityでオブジェクトを移動させるのに一般的な方法といえば、「transformによる移動」「Rigidbodyによる移動」「CharactorControllerによる移動」「Vectorによる移動」
だいたいこの4つが一般的なんじゃないかなーと思います。
一度に様々な移動方法が確認できれば便利じゃないかな?
ネットにもまだ無かったので、さまざまなオブジェクトの移動が確認できるシステムを作りました。
今回の使用したスクリプトは下記に記述したので、ぜひとも参考にして下さい。
開発環境
Unity 2020.1.f1 Personal
transformによる移動
オブジェクトの位置を直接書き換える方法。つまり、短いワープの連続でオブジェクトを動かしている。
そのためcolliderコンポーネントをアタッチしている場合、衝突判定をすり抜ける場合がある。
個人的にはあまりおススメできない移動方法。
transform.position
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
public float speed = 3.0f; void Update() { if (Input.GetKey("up")) { transform.position += transform.forward * speed * Time.deltaTime; } if (Input.GetKey("down")) { transform.position -= transform.forward * speed * Time.deltaTime; } if (Input.GetKey("right")) { transform.position += transform.right * speed * Time.deltaTime; } if (Input.GetKey("left")) { transform.position -= transform.right * speed * Time.deltaTime; } } |
transform.translate
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
public float speed = 3.0f; void Update() { if (Input.GetKey("up")) { transform.Translate(0, 0, speed * Time.deltaTime); } if (Input.GetKey("down")) { transform.Translate(0, 0, -speed * Time.deltaTime); } if (Input.GetKey("right")) { transform.Translate(speed * Time.deltaTime , 0, 0); } if (Input.GetKey("left")) { transform.Translate(-speed * Time.deltaTime , 0, 0); } } |
Rigidbodyによる移動
Rigdbodyコンポーネントをアタッチが必要。
今回は下記のように設定しました。

Rigidbody.AddForce
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public float speed = 3.0f; public Rigidbody rb; void Start() { rb = GetComponent<Rigidbody>(); } void FixedUpdate() { float x = Input.GetAxis("Horizontal") * speed; float z = Input.GetAxis("Vertical") * speed; rb.AddForce(x, 0, z); } |
Rigidbody.velocity
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
public float speed = 3f; float moveX = 0f; float moveZ = 0f; Rigidbody rb; void Start() { rb = GetComponent<Rigidbody>(); } void Update() { moveX = Input.GetAxis("Horizontal") * speed; moveZ = Input.GetAxis("Vertical") * speed; Vector3 direction = new Vector3(moveX, 0, moveZ); } void FixedUpdate() { rb.velocity = new Vector3(moveX, 0, moveZ); } |
Rigidbody.MovePosition
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
public float speed = 3f; public Rigidbody rb; void Start() { rb = GetComponent<Rigidbody>(); } void FixedUpdate() { if (Input.GetKey("up")) { rb.MovePosition(transform.position + transform.forward * Time.deltaTime * speed); } if (Input.GetKey("down")) { rb.MovePosition(transform.position + transform.forward * Time.deltaTime * -speed); } if (Input.GetKey("right")) { rb.MovePosition(transform.position + transform.right * Time.deltaTime * speed); } if (Input.GetKey("left")) { rb.MovePosition(transform.position + transform.right * Time.deltaTime * -speed); } } |
CharactorControllerによる移動
CharactorControllerコンポーネントのアタッチが必要
今回は以下のように設定しました。
rigidbodyに影響されずにゲームオブジェクトを操作できる。つまり、重力などの物理演算がきかなくなります。

CharactorController.SimpleMove
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public float speed = 3.0f; float moveX = 0f; float moveZ = 0f; CharacterController controller; void Start() { controller = GetComponent<CharacterController>(); } void Update() { moveX = Input.GetAxis("Horizontal") * speed; moveZ = Input.GetAxis("Vertical") * speed; Vector3 direction = new Vector3(moveX, 0, moveZ); controller.SimpleMove(direction); } |
CharactorController.Move
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
public float speed = 3.0f; public float gravity = 6.0f; private Vector3 moveDirection = Vector3.zero; CharacterController controller; void Start() { controller = GetComponent<CharacterController>(); } void Update() { if (controller.isGrounded) { moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); moveDirection = transform.TransformDirection(moveDirection); moveDirection *= speed; } moveDirection.y -= gravity * Time.deltaTime; controller.Move(moveDirection * Time.deltaTime); } |
Vectorによる移動
指定の位置まで一定速度で移動させる。
Vector3.MoveTowards
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
public float speed = 3.0f; Vector3 target; void Update() { if (Input.GetKey("up")) { target += new Vector3(0, 0, speed * Time.deltaTime); } if (Input.GetKey("down")) { target += new Vector3(0, 0, -speed * Time.deltaTime); } if (Input.GetKey("right")) { target += new Vector3(speed * Time.deltaTime, 0, 0); } if (Input.GetKey("left")) { target += new Vector3(-speed * Time.deltaTime, 0, 0); } float step = speed * Time.deltaTime; transform.position = Vector3.MoveTowards(transform.position, target, step); } |
Vector3.Lerp(Vector3.Slerp)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
public float speed = 3.0f; Vector3 target; void Update() { if (Input.GetKey("up")) { target += new Vector3(0, 0, speed * Time.deltaTime); } if (Input.GetKey("down")) { target += new Vector3(0, 0, -speed * Time.deltaTime); } if (Input.GetKey("right")) { target += new Vector3(speed * Time.deltaTime, 0, 0); } if (Input.GetKey("left")) { target += new Vector3(-speed * Time.deltaTime, 0, 0); } float step = speed * Time.deltaTime; transform.position = Vector3.Slerp(transform.position, target, step); //transform.position = Vector3.Lerp(transform.position, target, step); } |
まとめ
Unityでよく使われる移動方法について紹介しました。
どんな移動方法が良いか決める時の参考にしてみてください。
紹介した以外にオブジェクトの動かし方はあるので、自分に合ったものを見つけてくださいね。
それでは素敵なUnityゲーム開発ライフをー^^
参考URL:Unityでキャラクターを移動させる方法【3D編】
おすすめ記事 Unityは独学で勉強できる?【効果的なUnityの勉強法】
おすすめ記事【現役Unityエンジニアが厳選】Unityを学ぶのにおすすめのプログラミングスクール3選
おすすめのUnityスクール
Unityが学べるおすすめのプログラミングスクールはTECH STUDIAMになります。
理由はゲーム開発に特化したプログラミングスクールであり、2ヶ月で9万円の授業料で他のプログラミングスクールに比べても安いからです。
興味のある方は、無料相談してみるのをオススメます。
ゲーム制作を学べるオンラインスクール TECH STUDIAM