- Home /
Instantiate Onto An Empty Game Object Rotated
Hello, I'm trying for some time now to find a solution but i can't. I have a script that allows you to spawn a game object randomly onto an empty game object. It spawns the object i want fine on the position of the empty object but the rotation is 0 0 0. How can i spawn it but use the empty game object's rotation x y and z ?
Here is my script:
#pragma strict
var source : GameObject;
var spawnPoints : GameObject[];
function Start ()
{
var pos : Vector3 = spawnPoints[Random.Range(0, spawnPoints.Length)].transform.position;
var instance = Instantiate(source, pos ,transform.rotation);
}
Answer by duck · Jul 24, 2014 at 07:42 PM
This, I think, is what you're looking for:
#pragma strict
var source : GameObject;
var spawnPoints : GameObject[];
function Start ()
{
var spawnPoint = spawnPoints[Random.Range(0, spawnPoints.Length)].transform;
var instance = Instantiate(source, spawnPoint.position ,spawnPoint.rotation);
}
Answer by z3nth10n · Jul 24, 2014 at 07:27 PM
Maybe you can instantiate it first and later edit the position:
var go : GameObject = new GameObject();
This is the right way yo crate a new gameobject (a null one), using Instantiate won't work because, this is null, and Instantiate doesn't allow this kind of parameters
go.transform.rotation = Quaternion.Euler(30, 0, 0);
This will rotate the gameobject 30 degrees in the x-axis
So, you would use better this code:
#pragma strict
var source : GameObject;
var spawnPoints : GameObject[];
function Start ()
{
var go : GameObject = new GameObject();
var pos : Vector3 = spawnPoints[Random.Range(0, spawnPoints.Length)].transform.position;
go.transform.position = pos;
go.transform.rotation = Quaternion.Euler(30, 0, 0);
}
Bye. ^^
Sorry, no. This does not work at all. It wont even spawn the game object at all.