- Home /
Instantiate a prefab with a key press to a relative location?
I'm making a 2D infinite runner game. I've created a prefab called Block that I want to spawn into the level whenever I press the X key. I want it to spawn at 15, 1.5, 0, which is 15 relative to the Player since they are always moving forward and 1.5 relative to the ground since the player will be jumping and I don't want the blocks to spawn in the air.
Below is the javascript I have right now, which creates the prefab at 0,0,0. I don't know how to deal with the problem of the Player always running and jumping. The script is attached to the main camera, which is attached to the player; if that is important. I'm still a beginner, so I'd appreciate as much detail as possible, and thanks!
#pragma strict
var objectName : GameObject;
function Start()
{
}
function Update()
{
if(Input.GetKeyDown(KeyCode.X)) {
var pos ; Vector3 (0,0,0);
var rot : Quaternion = Quaternion.identity;
Instantiate(objectName);
}
}
Answer by getyour411 · Mar 10, 2014 at 01:59 AM
pos : Vector3(player.transform.position.x+15, player.transform.position.y+1.5,0);
Instantiate(objectName, pos, rot);
I added this in where I thought I am supposed to, but I keep getting errors no matter what I change.
#pragma strict
var objectName : GameObject;
var player : GameObject;
function Start()
{
}
function Update()
{
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.X)) {
pos : Vector3(player.transform.position.x+15,player.transform.position.y+1.5,0);
var rot : Quaternion = Quaternion.identity;
Instantiate(objectName, pos, rot);
}
}
It's telling me that pos is an unexpected token. I added the variable "player", because I was getting some other error. Also, what is "player" in "player.transform.position" a reference to? Is that where the name of the player is supposed to be? Because the name of the player gameobject is Character and is tagged as Player. Sorry, I'm just bad at this.
$$anonymous$$ake sure you have dragged your player into the player field in Inspector; the name or tag is irrelevant. I don't do JS but maybe you need the keyword 'var' in front of the pos = ... bit
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.X)) {
var pos : Vector3;((player.transform.position.x+15) (player.transform.position.y+1.5,0));
var rot : Quaternion = Quaternion.identity; // Quaternion.identity essentially means "no rotation"
Instantiate(objectName, pos, rot);
}
}
I added var and then it said I needed a semi colon after Vector3 and then it didn't like the comma and lack of parenthesis. So, this is what it looks like now and it's saying " It is not possible to invoke an expression of type 'float'". Hmmm it's getting late so I'm gonna have to sleep on this, but what did you mean by "dragged your player into the player field in Inspector"?
You put a semi-colon after Vector3, remove that; you put parenthesis around the Vector3 arguments, undo all that; I can't really $$anonymous$$ch you JS syntax line by line in this space, some basics are assumed/needed.
Regarding player, you setup a variable named player when you added the line var player : GameObject;
In the Inspector, if you look at the GameObject this script is attached to you'll see a field named player; drag your Player (from the Project hierarchy/windows next to scene) into that field. Again, that's pretty much Unity Day 1 app basics, you might really benefit from a few tutorials - I wish you luck.
Finally got it, thanks a ton!
#pragma strict
var objectName : GameObject;
var player : GameObject;
function Start()
{
}
function Update()
{
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.X)) {
var pos = Vector2(15,2);
var rot : Quaternion = Quaternion.identity;
Instantiate(objectName,pos+player.transform.position, rot);
}
}
Link to my other question with same problem: http://answers.unity3d.com/questions/660701/instantiate-to-relative-location.html