甘酒のアプリ開発

一人でも多くの人を幸せにするアプリを作っていけたらなぁと思います。

【Unity】transform.positionによるオブジェクトの移動をスクリプトから制御する方法

transform.positionによるオブジェクトの移動をスクリプトから制御する方法をまとめてみました。

 

回転を制御したい場合はこちらをご覧ください。(_ _)

  

 

transform.positionによる移動の制御

 

new Vector3(pos.x + 1f, pos.y, pos.z);

 

説明:右に進む

this.gameObject.transform.position = new Vector3(pos.x + 1f, pos.y, pos.z);

  • サンプルコード
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class migiidou : MonoBehaviour{
  5.     void Update()
  6.     {
  7.         Vector3 pos = this.gameObject.transform.position;
  8.         this.gameObject.transform.position = new Vector3(pos.x + 1f, pos.y, pos.z);
  9.     }
  10. }

 

 -------------------------------------------------------------------------------------------------

new Vector3(pos.x - 1f, pos.y, pos.z);

 

説明:左に進む

this.gameObject.transform.position = new Vector3(pos.x - 1f, pos.y, pos.z);

  • サンプルコード
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class hidariidou : MonoBehaviour{
  5.     void Update()
  6.     {
  7.         Vector3 pos = this.gameObject.transform.position;
  8.         this.gameObject.transform.position = new Vector3(pos.x - 1f, pos.y, pos.z);
  9.     }
  10. }

 

 -------------------------------------------------------------------------------------------------

new Vector3(pos.x, pos.y, pos.z + 1f);

 

説明:前に進む

this.gameObject.transform.position = new Vector3(pos.x, pos.y, pos.z + 1f);

  • サンプルコード
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class public class maeidou : MonoBehaviour{
  5.     void Update()
  6.     {
  7.         Vector3 pos = this.gameObject.transform.position;
  8.         this.gameObject.transform.position = new Vector3(pos.x, pos.y, pos.z + 1f);
  9.     }
  10. }

 

 -------------------------------------------------------------------------------------------------

 

new Vector3(pos.x, pos.y, pos.z - 1f);

 

説明:後ろに進む

this.gameObject.transform.position = new Vector3(pos.x, pos.y, pos.z - 1f);

  • サンプルコード
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class public class ushiroidou : MonoBehaviour{
  5.     void Update()
  6.     {
  7.         Vector3 pos = this.gameObject.transform.position;
  8.         this.gameObject.transform.position = new Vector3(pos.x, pos.y, pos.z - 1f);
  9.     }
  10. }

 

 -------------------------------------------------------------------------------------------------

new Vector3(pos.x, pos.y + 1f, pos.z);

 

説明:上に進む

this.gameObject.transform.position = new Vector3(pos.x, pos.y + 1f, pos.z);

  • サンプルコード
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class public class ueidou : MonoBehaviour{
  5.     void Update()
  6.     {
  7.         Vector3 pos = this.gameObject.transform.position;
  8.         this.gameObject.transform.position = new Vector3(pos.x, pos.y + 1f, pos.z);
  9.     }
  10. }

 

 -------------------------------------------------------------------------------------------------

new Vector3(pos.x, pos.y - 1f, pos.z);

 

説明:下に進む

this.gameObject.transform.position = new Vector3(pos.x, pos.y - 1f, pos.z);

  • サンプルコード
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class public class shitaidou : MonoBehaviour{
  5.     void Update()
  6.     {
  7.         Vector3 pos = this.gameObject.transform.position;
  8.         this.gameObject.transform.position = new Vector3(pos.x, pos.y - 1f, pos.z);
  9.     }
  10. }