- Home /
Spawning portal
I have a prefab called portal and I want to make it so that when my player press the P button it makes a portal appear infront of them. I would like to make it so that it is a little infront of them that way you dont instantly go though it.
I have tried this but it never spawns my object.
var prefab : Transform;
if(Input.GetKey(KeyCode.D)) {
Instantiate (prefab, Vector3(2.0, 0, 0));
}
"prefab" should be a GameObject, like this:
var prefab : GameObject;
Then, in the inspector, drag your prefab into the "prefab" variable on your script.
I've gotten prefabs to work as Transforms, I'm not aware of a performance difference but it works for me :)
Where is that if condition stationed? It has to be in some kind of Update function. But also, the Vector3(2.0, 0, 0) is going to cause it to be 2 units to the right of the center of the world.
If your character is far from the center, you may be unaware that it's spawning.
You can Instantiate into many different types :
#pragma strict
var theCubeT : Transform;
var theCubeG : GameObject;
var theCubeS : CubeScript;
function Update () {
if( Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.T) )
{
Instantiate( theCubeT, transform.position + (transform.forward * 2.0), Quaternion.identity );
}
if( Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.G) )
{
Instantiate( theCubeG, transform.position + (transform.forward * 2.0), Quaternion.identity );
}
if( Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.S) )
{
Instantiate( theCubeS, transform.position + (transform.forward * 2.0), Quaternion.identity );
}
}
Right, but isn't the type important? For instance, if he's spawning a portal, it's probably not a "Transform", but rather some type of GameObject.
Or does the type not matter for prefab variables?
It really just depends on the kind of reference you want to keep and use in your script. If you are just checking position or manipulating position , then store and use a reference to the transform. If you are using the variables on that script, clone to the script type, you can still step up through the components to the transform (or gameObject).
From memory, transform is an external call ( gameObject.transform ) so it is better to store a local reference (cache) a transform . For this purpose, the clone is not instantiated into a variable or array, so the next Update the object is lost anyway =]
Answer by AlucardJay · Oct 10, 2012 at 11:57 PM
Ok, a couple of problems here. Check the instantiate command first :
http://docs.unity3d.com/Documentation/ScriptReference/Object.Instantiate.html
Instantiate (original : Object, position : Vector3, rotation : Quaternion) : Object
it says you need to provide an object, a position and a rotation. The easiest way to do this is :
Instantiate( original , transform.position, Quaternion.identity );
this will clone 'original' at the position and at a rotation of zero.
to make something be 2 units in front of something, use this formula :
transform.position + (transform.forward * 2.0)
put it all together and you have :
#pragma strict
var prefab : Transform;
function Update () {
if( Input.GetKeyDown(KeyCode.D) )
{
Instantiate( prefab, transform.position + (transform.forward * 2.0), Quaternion.identity );
}
}
And as I said in my comment, you can Instantiate into many types =]
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Please Help Convert js to c# 1 Answer
Procedural Generation 1 Answer
Distribute terrain in zones 3 Answers
PinBall Flippers 1 Answer