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);
- サンプルコード
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class migiidou : MonoBehaviour{
- void Update()
- {
- Vector3 pos = this.gameObject.transform.position;
- this.gameObject.transform.position = new Vector3(pos.x + 1f, pos.y, pos.z);
- }
- }
-------------------------------------------------------------------------------------------------
new Vector3(pos.x - 1f, pos.y, pos.z);
説明:左に進む
this.gameObject.transform.position = new Vector3(pos.x - 1f, pos.y, pos.z);
- サンプルコード
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class hidariidou : MonoBehaviour{
- void Update()
- {
- Vector3 pos = this.gameObject.transform.position;
- this.gameObject.transform.position = new Vector3(pos.x - 1f, pos.y, pos.z);
- }
- }
-------------------------------------------------------------------------------------------------
new Vector3(pos.x, pos.y, pos.z + 1f);
説明:前に進む
this.gameObject.transform.position = new Vector3(pos.x, pos.y, pos.z + 1f);
- サンプルコード
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class public class maeidou : MonoBehaviour{
- void Update()
- {
- Vector3 pos = this.gameObject.transform.position;
- this.gameObject.transform.position = new Vector3(pos.x, pos.y, pos.z + 1f);
- }
- }
-------------------------------------------------------------------------------------------------
new Vector3(pos.x, pos.y, pos.z - 1f);
説明:後ろに進む
this.gameObject.transform.position = new Vector3(pos.x, pos.y, pos.z - 1f);
- サンプルコード
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class public class ushiroidou : MonoBehaviour{
- void Update()
- {
- Vector3 pos = this.gameObject.transform.position;
- this.gameObject.transform.position = new Vector3(pos.x, pos.y, pos.z - 1f);
- }
- }
-------------------------------------------------------------------------------------------------
new Vector3(pos.x, pos.y + 1f, pos.z);
説明:上に進む
this.gameObject.transform.position = new Vector3(pos.x, pos.y + 1f, pos.z);
- サンプルコード
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class public class ueidou : MonoBehaviour{
- void Update()
- {
- Vector3 pos = this.gameObject.transform.position;
- this.gameObject.transform.position = new Vector3(pos.x, pos.y + 1f, pos.z);
- }
- }
-------------------------------------------------------------------------------------------------
new Vector3(pos.x, pos.y - 1f, pos.z);
説明:下に進む
this.gameObject.transform.position = new Vector3(pos.x, pos.y - 1f, pos.z);
- サンプルコード
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class public class shitaidou : MonoBehaviour{
- void Update()
- {
- Vector3 pos = this.gameObject.transform.position;
- this.gameObject.transform.position = new Vector3(pos.x, pos.y - 1f, pos.z);
- }
- }