ゲームオブジェクトの操作についてまとめます。
この記事で行う操作は以下の通りです。
位置変更 .transform.localPosition = new Vector3
x軸のみ1.0の位置に移動します。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LocalPositionChange : MonoBehaviour
{
public GameObject player;
// Start is called before the first frame update
void Start()
{
player.transform.localPosition = new Vector3(1f, 0, 0);
}
}
ゲーム開始時に x=1.0, y=0, z=0の位置に移動します。

移動 .transform.Translate (vector3)
1フレームごとに0.001移動します。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Translate : MonoBehaviour
{
public GameObject player;
// Update is called once per frame
void Update()
{
player.transform.Translate(0.001f, 0, 0);
}
}

生成 Instantiate(ゲームオブジェクト);
アタッチされたゲームオブジェクトを生成します。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectGenerator : MonoBehaviour
{
public GameObject playerObject;
// Start is called before the first frame update
void Start()
{
Instantiate(playerObject);
}
}
オブジェクトが(0,0,0)の位置に生成されます。

位置情報を含んだ生成 Instantiate(オブジェクト,位置,回転);
Instantiateの引数で生成する位置を指定します。
第3引数のQuaternion.identity 回転しない意味になります。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectGenerator : MonoBehaviour
{
public GameObject playerObject;
// Start is called before the first frame update
void Start()
{
Vector3 playerPos = new Vector3(1f, 0, 0);
Instantiate(playerObject, playerPos, Quaternion.identity);
}
}
x=1.0, y=0, z=0の位置にオブジェクトを生成。

Quaternion.Euler で回転させます。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectGenerator : MonoBehaviour
{
public GameObject playerObject;
// Start is called before the first frame update
void Start()
{
Vector3 playerPos = new Vector3(1f, 0, 0);
Quaternion playerRotate = Quaternion.Euler(0, 0, 30);
Instantiate(playerObject, playerPos, playerRotate);
}
}
z軸に対して、30°回転した状態で生成されます。

ランダムな位置に生成 Random.Range(最小値,最大値);
Random.Rangeを使って、ランダムな位置に生成します。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectGenerator : MonoBehaviour
{
public GameObject playerObject;
// Start is called before the first frame update
void Start()
{
Vector3 playerPos = new Vector3(Random.Range(-2f,2f),0,0);
Instantiate(playerObject, playerPos, Quaternion.identity);
}
}
x軸に対して、 -2から2までの範囲内でランダムにオブジェクトを生成します。

インスペクターで確認すると、x軸は0.897…で生成されました。

オブジェクトを消す Destroy(オブジェクト);
アタッチしたゲームオブジェクトを2秒後に消します。
第2引数を設定しない場合は、即時に消します。

オブジェクトを見つける Find(“”);
ゲーム中に生成されたオブジェクトは最初にオブジェクトをアタッチしておくことができません。
その場合は、Findでオブジェクトを取得します。
下記ではStart関数で生成されたオブジェクトを2秒後に削除しています。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectDelete : MonoBehaviour
{
private GameObject target;
// Start is called before the first frame update
void Update()
{
target = GameObject.Find("Player(Clone)");
Destroy(target,2);
}
}
ただ、上記の方法ではFindの名前にあるように生成したオブジェクト名に完全一致するよう、
(Clone)と記載しないと見つけてもらえません。
Instanceする際にオブジェクト名を変更する方法もあるようですが、複数のオブジェクトを扱う時に
あまり現実的ではないと思うので、FindGameObjectWithTagを使用して、Playerタグのついた
オブジェクトを発見するようにしました。
タグのついたオブジェクトを見つける
まずは、生成するオブジェクトのタグを設定します。

以下は、Playerタグのついたオブジェクトを発見して、2秒後に削除しています。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectDelete : MonoBehaviour
{
private GameObject target;
// Start is called before the first frame update
void Update()
{
target = GameObject.FindGameObjectWithTag("Player");
Destroy(target,2);
}
}

コメント