- Home /
How to create a loop level?
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.
Answer by Reshad · Jun 24, 2013 at 05:52 AM
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);
}
}
}
Answer by AlucardJay · Jun 20, 2013 at 08:13 AM
// 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;
Thank you for your answer. But when I use rot = Quaternion.Euler( 0, 90, 0 ); my editor y-axis rotation value shows 90.00001. How can I solve it? I can't find any exact solution for that.
I have a arc model of a circle and I want to load it from my resource and at the end of the arc I set a trigger. When the player touches that trigger I want to instantiate that model again with different rotation and join the vertex of that model with previous model. Can anyone tell me how to join the second model's vertex with the first model in coding? 
Wow, sorry, I never received a reply email until now. It looks like Unity Answers email notification has not been working again. (the 'site was down for maintenance twice this weekend)
Am glad you got the problem solved. You get upvote from me for your persistence!
Regarding the floating point error, I cannot explain that. Unity is strange sometimes when it comes to euler rotations.
Your answer
Follow this Question
Related Questions
World building advice for 3.5D platformer 0 Answers
Stacking level blocks in an Infinite runner game 2 Answers
How much of a video game's art is made in the engine and how much from external programs? 1 Answer
Using custom Transform/GameObject classes? 0 Answers
Model in Blender, or Block in Unity? 0 Answers