- Home /
How can I make a prefab be Intantiated in a specific point of my player? I'm using Unity in 2D
So, I'm trying to Instantiate a prefab object, in this case a Sword, in front of my player (which in this prototype is a sprite of a green square)
To make the sword appear I used this code (Javascript):
#pragma strict
public var Sword : Rigidbody2D; //It specifies that the selected prefab is a Rigidbody2D.
public var playerEnd : Transform; //This works as the reference for the location in which the weapon will appear.
function Start () {
}
function FixedUpdate () {
if(Input.GetKeyDown(KeyCode.J)){ //GetKeyDown for once the button/key is pressed, to activate the event again the player must press the button/key again.
var swordInstance : Rigidbody2D; //This will summon the object as a rigidbody2D
swordInstance = Instantiate(Sword /*This is the prefab*/ , playerEnd.position /*This is rhe position in which the prefab will be created*/, playerEnd.rotation /*This will stablish the orientation of the object, depends on the orientation of the player*/);
}
}
For the playerEnd I tried to put my Game Object of my player, the Green Square, and it works (Kind of...), but the sword appear in the middle of the player.
Just like this...
Soooo, how can I make the thing to appear in front of the player? Is there a way to do it?
Something to try would be to create the sword as a child object of the player. Then you'll be able to move the sword's position by changing the position of the child object. And since it's a child of the player it will stay with the player at all times. :)
Never thought about that before O.O $$anonymous$$ight as well try it ;) Thanks Sterling!
Np, crazy what you can take from some tutorials! :)