- Home /
Set the orientation of a Prefab
So i have some Prefabs which will be my Level Parts. How to Spawn them in rotated ? I am also spawning the same Prefab multiple times. So i want it one time in one direction one other time in another:
Here is a Example:
Thanks for you help.
Greetings
Teiwaz
$$anonymous$$ore information is required to give an answer
Answer by amphoterik · Jul 08, 2013 at 12:30 PM
The instantiate method allows you to specify a position as well as a rotation. Therefore, if you new the rotation and position you wanted, you could write:
Instantiate(prefab, position, rotation);
This allows you to make multiple objects each with their own rotation. More info can be found here: http://docs.unity3d.com/Documentation/ScriptReference/Object.Instantiate.html
An easy way to make a rotation is: 0 degrees is Quaternion.identity
(this is listed in the Instantiate docs.) 90 degrees is Quaternion.Euler(0,90,0);
and so on.
They go where "rotation" is.
Or, if you hate long lines, before Instantiate put: `Quaternion rotation = Quaternion.Euler(0,180,0);
Current no access to the code. Will post the chunk later.
Greetings
Teiwaz
That way i set the rotation of a part.
public void RotateLevel(Comp$$anonymous$$atrix.direction rotation){
switch(rotation) {
case Comp$$anonymous$$atrix.direction.LEFT:
transform.rotation= Quaternion.Euler(new Vector3(0,90,0));
my$$anonymous$$atrix.RotateComp$$anonymous$$atrix(Comp$$anonymous$$atrix.direction.LEFT);
//Debug.Log("Rotate Left");
break;
case Comp$$anonymous$$atrix.direction.RIGHT:
transform.rotation= Quaternion.Euler(new Vector3(0,-90,0));
my$$anonymous$$atrix.RotateComp$$anonymous$$atrix(Comp$$anonymous$$atrix.direction.RIGHT);
//Debug.Log("Rotate Right");
break;
case Comp$$anonymous$$atrix.direction.TURN:
transform.rotation= Quaternion.Euler(new Vector3(0,180,0));
my$$anonymous$$atrix.RotateComp$$anonymous$$atrix(Comp$$anonymous$$atrix.direction.LEFT);
my$$anonymous$$atrix.RotateComp$$anonymous$$atrix(Comp$$anonymous$$atrix.direction.LEFT);
//Debug.Log("Rotate 180°");
break;
default:
Debug.Log("Unknow Direction");
break;
}
}
and then i simply instatiate it that way:
theLevel[x,z] = Instantiate(theLevel[x,z],new Vector3(x * theLevel[x,z].transform.renderer.bounds.size.x, 0, z * theLevel[x,z].transform.renderer.bounds.size.z), theLevel[x,z].transform.rotation) as GameObject;
Greetings
Teiwaz
Answer by sparkzbarca · Jul 08, 2013 at 12:31 PM
instantiate(prefab,position,rotation)
change rotation to change its rotation.
you can just do a normal rotation plus or minus 90/180 degrees to do a simple rotation
But how to save the Rotation of a prefab before ?
Iam scanning my level and setting the rotation for each GameObject with:
transform.rotation= Quaternion.Euler(new Vector3(0,90,0));
And later simply instatiate it like you said before. But this didnt work. He everytime use the same Rotation.
@Teiwaz What you said was literally: "I'm setting the rotation of all my objects to be the same, but it's not working... they are all the same." ???
dat logic
based on the code i can see what your trying to do.
You want to rotate the level but like be able to press a left twice for example and rotate 180 degrees.
the easiest way to rotate is
if(RotateLeft)
Angle = 90;
else if(RotateRight)
angle = -90;
else if (UTurn)
angle = 180;
transform.rotation.rotate(new vector3(0,angle,0));
Not realy what i want to do but basicly you hit the point. i created a level generator. You can define a maxSize ( 100x100 Chunks). Also you define a filling%. The level is filled with empty Chunks. Then i set the filled Chunk as like a Ball trown in a 2- Dimensional Array. This Ball bounces random within the Array Fileds, and slow the Level (the white fields are beeing applyed where the level is filled).
In the second funktion i check a compatibility $$anonymous$$atrix of each filled part agains different prefabs. If the are equal i set the prefab at the current position. To ensure that the Part fit / didnt fit, i need to ROTATE the prefab. Cause like on the Screenshot you can see the bottom and the top one are the same just rotated. So normaly i just need to set the rotation of the Prefab.
This ONLY works if i do this direct at the time i SCAN the parts. If i try to scan the level first and after that in a second funktion instatiate the Prefab with nearly the SA$$anonymous$$E code has a different effekt !
theLevel[x,z] = Instantiate(go,new Vector3(x * go.transform.renderer.bounds.size.x, 0, z * go.transform.renderer.bounds.size.z),Quaternion.AngleAxis(go.GetComponent<LVL_Patch>().myRotation - 180,Vector3.up)) as GameObject;
<- That way it works. But i want the code to be more modular. So i want first scan, safe the right prefab and instatiate it with the right rotation in a second funktion. And this doesnt work.
So you have a method that works, you just want to split it up, and that doesn't work? How about telling us more about "this doesn't work", rather than complaining about it