Kanade Labo

かなで研究所

{unity:script/memo}Vector3型の変数を別の変数に代入する

Unity初心者の かなで がネットで調べて、実践できた知識の覚書。
基本的に自分用備忘録の為、説明不備はご了承くださいm(_ _)m

黒字:デフォルト
赤字:今回追加
青字:自己解釈(一般解釈とはかけ離れてる事に注意!)

目標:Vector3という型の変数aaaに代入する方法(aaa=0,0,0みたいなのはNG)

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

public class sample : MonoBehaviour {
        Vector3 aaa;    //aaaをVector3という型にしておく。

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		aaa = new Vector3(0.0f,0.0f,0.0f);
                //new Vector3(x軸,y軸,z軸)という書き方で代入出来る。
                //あと、数字の後は「f」を付けないとだめ。
                Debug.Log ("aaa");
	}
}

目標:上のaaaをText型/int型/String型に代入する

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;    //Text型を使うときだけ必要

public class sample : MonoBehaviour {
        Vector3 aaa;    //aaaをVector3という型にしておく。
        public Text ddd;    //Text型を使うときだけ必要
	// Use this for initialization
	void Start () {
		ddd = this.GetComponent<Text> ();    //Text型を使うときだけ必要
	}
	
	// Update is called once per frame
	void Update () {
		aaa = new Vector3(0.0f,0.0f,0.0f);
                int bbb = (int)aaa[0]    //[0]はx軸のみ、[1]はy軸のみ、[2]はz軸のみ取り出せる。小数点以下は出ない。
                string ccc = ""+aaa;    //ccc="(0.0,0.0,0.0)"という感じでかっこ()も一緒に代入される
                string ccc = ""+aaa[0];    //これなら数値だけ取り出せる。結果はint型と一緒
                ddd.text = ""+aaa;    // Textコンポーネントに表示する。結果は"(0.0,0.0,0.0)"
	}
}

-unity