Vector3型の変数から長さを取得したい場合にmagnitudeを使います。
ベクトルの長さを返します。速度や3次元座標はベクトルのため、そのままでは数値と比較することはできないため、magnitudeでfloat型として取得します。
float
speed = rb.velocity.magnitude;
NavMeshAgentのオブジェクトが移動しているかどうか、は
NavMeshAgentのvelocity.magnitude
で判断します。「velocity.magnitude」が0よりも大きければ移動していることになる。
関連:
Vector3.normalized
正規化、magnitude(長さ)を1としたベクトル(単位ベクトル)を返します。返り値はVector3。
現在のベクトルの方向を維持したまま、長さが1のベクトルを作成したい場合に使用します。
]]>
Obj = (GameObject)Instantiate (Flame, this.transform.position, Quaternion.identity);
Obj.transform.parent = Player.transform;
・やりかた2(Unity5よりInstantiateの4つめの引数が追加された)
var parent = this.transform;
Instantiate(childPrefab, Vector3.zero, Quaternion.identity, parent);
Instantiate関係なく、子オブジェクトにする方法は2つある
1.transform.parent
transform.parent = GameObject.Find("WannaBeParent").transform;
子オブジェクトから親オブジェクトを解除するにはtransform.parentをnullにすることで親子関係を解除します。
ChildObject.transform.parent = null;
親オブジェクトから子オブジェクトの全てを解除する場合、DetachChildren()を使用すれば一括で解除できます。
RootObject.transform.DetachChildren();
2.transform.SetParen
transform.SetParent( parent, true );
SetParent関数を使うと、第二引数てtrueを指定すると、
ローカル座標を維持したまま親オブジェクトを設定できる
※基本的にはこっちを使うこと
]]>
// 比較
if (v1 == v2){
Debug.Log ("v1 = v2");
}
オブジェクトにおけるZ軸のプラス方向が正面として、前、後への移動などを設定する便利なVector3の値が用意されています。「それぞれ値が+1」されます。
方向 値
前方向 Vector3.forward
後方向 Vector3.back
左方向 Vector3.left
右方向 Vector3.right
上方向 Vector3.up
下方向 Vector3.down
>そのオブジェクトの正面
transform.forward
this.transform.position;
自分の向いている方向(ベクトル)
this.transform.forward;
]]>
いくぶん処理を軽くするにはTerrainを単なるMeshにする必要があります。
Terrain To Meshというアセット(有料、27ドル)を使用。
https://www.assetstore.unity3d.com/jp/#!/content/47276
1.アセットを読み込んでウインドウを表示。
2.指定のTerrainを読みこんでもろもろ設定。
・Meshにしたときの名前をつける
3.画面下のGenerateボタンを押す。
完了。
※逆にMesh To Terrainというアセットもあるらしい。自分は未使用だがアセットストアで販売されている高品質のTerrainを自分のプロジェクトに都合よく変えるにはいいかもしれない。
https://www.assetstore.unity3d.com/jp/#!/content/7271
]]>
参照元の外から見たい変数を
public static
で宣言。
あとは、
クラス名.変数名でアクセス。
やり方2)
※上記の方法が使えない場合
1.ゲームオブジェクトを宣言。
public GameObject Obj;
2.参照したいゲームオブジェクトを上記のインスペクタにアタッチ
3.
被参照クラス名 Obj2 = Obj.GetComponent<被参照クラス名>();
Obj2.変数名
でアクセス。
]]>
using UnityEngine;
using System.Collections;
public class come : MonoBehaviour {
public Transform car;
NavMeshAgent agent;
// Use this for initialization
void Start () {
agent = GetComponent<NavMeshAgent>();
}
// Update is called once per frame
void Update () {
Vector3 dir = car.transform.position - this.transform.position;
Vector3 pos = this.transform.position + dir * 1.5f;
pos.z -= 30;
this.transform.rotation = Quaternion.LookRotation(dir);
agent.destination = pos;
}
void OnTriggerEnter(Collider other){
GetComponent<Animator>().SetBool("Roar",true);
}
void OnTriggerExit(Collider other){
GetComponent<Animator>().SetBool("Roar",false);
}
}
インスペクタの設定:
Materialsで軌跡のパーティクルを指定。
Time
は軌跡がフェードアウトするまでの時間。Start Width
とEnd Width
はそれぞれ軌跡開始幅と軌跡終端幅。