- Home /
Rotating specific prefab - instantiated with Random.Range
I have instantiated my Prefabs using Random.Range.(different types of prefabs) Out of the different instantiated prefabs i want to rotate a specific prefab.
Any help would be appreciated..Thank you
Answer by RustyCrow · Aug 24, 2018 at 11:30 AM
Not coding atm but assuming a small range(0 - 2)
Edit: replacing with actual code and a better explanation. So reading your response @Sanjay1987 i think you are just Rotating on wrong angle, try Y for horizontal, X will make like a ramp and Z is a barrel role :P idk. But for the code (keep in mind i dont know your code setup/structure so this might not work for you) I also might have misunderstood your question in case you want to do operations after spawn see the comments in the code.
void Start ()
{
for (int i = 0; i < Prefabs.Length; i++)
{
var result = Random.Range(0,2); // This is ouer prefab value
// Alt ver if you want to spawn first Then do change
// var go = insta(pref[res])
// we determin ouer Prefab(in this case i want the prefab at index 1 to be tilted)
if (result == 1 )
{
// angle the prefab on index 1
Instantiate(Prefabs[result], transform.position, Quaternion.Euler(new Vector3(0, 45, 0)));
//go.trans.rot = Quaternion.Euler(new Vector3(0, 45, 0))
}
else
{
Instantiate(Prefabs[result], transform.position, Quaternion.identity);
// go.trans.rot = Quaternion.identity
}
}
}
The assumption here is that you are using Random value in a Array to determine which prefab to spawn. If we now save that number and say if this value shows up then do something els with the Gameobject(like rotating it) else just do a standard spawn. This is a very basic answer if you need more complex one i think we need to see your script(s).
How would this address "i want to rotate a specific prefab." after instantiating the prefabs part, as it seems like he wants to do this after the instantiation.
Hmm i must be misunderstanding the question then. The specific prefab would be the value you would put in the if statement(over case 1) then do the rotation with instantiate parameter or i guess do it after instantiating
var go = insta(prefab)
go.trans.rotation = new rotation
Answer by Sanjay1987 · Aug 24, 2018 at 11:41 AM
@RustyCrow Thank you. I did the exact same thing. But instead the object won't rotate along the x-axis.(like when you collect coins, coins rotate horizontally). Sorry if this tends to become a new topic. After finding the specific instance using the above method. i wrote this.
Instantiate (prefabs[result, newTransform.position, Quaternion.Euler (45, 0, 0));
But this just slants the prefab instead of rotating it.
Anyway thanks for you help.
Try changing the value on Y (0,45,0) (X, Y, Z), if still not working check out the updated answer
@RustyCrow Thank you very much.. Sorry if this is a lame question. I have set my project in 2d mode. When i switch it to 3d i can see the prefab rotating on all axes. But when i switch back to 2d, the 3d effect disappears. So has it got to do anything with my project settings. Or could i get a 3d effect in a 2d project?
Your answer
Follow this Question
Related Questions
Photon Instantiated Prefab of OTHER Player Falls Over 1 Answer
nee help with Instantiating prefab at mouse position with GUI button 0 Answers
How do you spawn multiple prefabs within a certain distance from each other from one spawn point? 2 Answers
Accessing Script on instated prefab 1 Answer
Assign a 'Transform' target to prefab by finding a 'GameObject' 1 Answer