Hi guys,
I want to randomly instantiate a prefab under a particular game object with specific rotation. For more details I have 12 level prefabs on a circle segment model. I want to instantiate these prefabs randomly with rotation 0, 90(y-axis), 180(y-axis), 270(y-axis) when player touches a trigger. So the entire level will change the prefabs clockwise and a loop will be created. But I don’t know how exactly I can do it. Any help will be much appreciated. Thank you.
// when the player hits the trigger
var rot : Quaternion;
switch( Random.Range( 0, 4 ) )
{
case 0 :
rot = Quaternion.Euler( 0, 0, 0 );
break;
case 1 :
rot = Quaternion.Euler( 0, 90, 0 );
break;
case 2 :
rot = Quaternion.Euler( 0, 180, 0 );
break;
case 3 :
rot = Quaternion.Euler( 0, 270, 0 );
break;
}
object.rotation = rot;
Finally I got it working. I have placed four trigger on the road end position and instantiated prefab randomly when player touches the trigger and destroy the previous one. For that a loop circle level has been created what I wanted.
using UnityEngine;
using System.Collections;
public class LevelChange : MonoBehaviour {
public int randomChange;
private int startLevel = 0;
private int endLevel = 11;
public Transform s1;
public Transform s2;
public Transform s3;
public Transform s4;
public GameObject[] level_prefab = new GameObject[12];
void Start() {
NotificationCenter.DefaultCenter.AddObserver(this, "TriggerA");
NotificationCenter.DefaultCenter.AddObserver(this, "TriggerB");
NotificationCenter.DefaultCenter.AddObserver(this, "TriggerC");
NotificationCenter.DefaultCenter.AddObserver(this, "TriggerD");
s1 = GameObject.Find("S1").transform; //0-degree
s2 = GameObject.Find("S2").transform; //90-degree
s3 = GameObject.Find("S3").transform; //180-degree
s4 = GameObject.Find("S4").transform; //270-degree
GameObject l1 = Instantiate(level_prefab[startLevel], s1.transform.position, s1.transform.rotation) as GameObject;
l1.transform.parent = s1.transform;
GameObject l4 = Instantiate(level_prefab[endLevel], s4.transform.position, s4.transform.rotation) as GameObject;
l4.transform.parent = s4.transform;
}
void Update() {
randomChange = Random.Range(0,12);
//Debug.Log(randomChange);
}
public void TriggerA() {
GameObject l2 = Instantiate(level_prefab[randomChange], s2.transform.position, s2.transform.rotation) as GameObject;
l2.transform.parent = s2.transform;
foreach(Transform child in s4) {
Destroy(child.gameObject);
}
}
public void TriggerB() {
GameObject l3 = Instantiate(level_prefab[randomChange], s3.transform.position, s3.transform.rotation) as GameObject;
l3.transform.parent = s3.transform;
foreach(Transform child in s1) {
Destroy(child.gameObject);
}
}
public void TriggerC() {
GameObject l4 = Instantiate(level_prefab[randomChange], s4.transform.position, s4.transform.rotation) as GameObject;
l4.transform.parent = s4.transform;
foreach(Transform child in s2) {
Destroy(child.gameObject);
}
}
public void TriggerD() {
GameObject l1 = Instantiate(level_prefab[randomChange], s1.transform.position, s1.transform.rotation) as GameObject;
l1.transform.parent = s1.transform;
foreach(Transform child in s3) {
Destroy(child.gameObject);
}
}
}