- Home /
Question by
Trollvahkiin · Feb 08, 2014 at 05:47 PM ·
instantiatespawn
How to instantiate object infront of player?
Hey, how could I instantiate an object in front of the player. I checked the reference for Instantiate and it can have a position in vector3. How could I simply spawn an object 5 units in front of my player? Thanks.
Comment
Best Answer
Answer by robertbu · Feb 08, 2014 at 05:54 PM
Assuming 3D and by 'in front' you are referring to the side facing positive 'z' when rotation is (0,0,0):
Instantiate(prefab, transform.position + transform.forward * 5, Quaternion.Identity);
How would you spawn an object when the rotation is not at (0,0,0)
Answer by Stormizin · Feb 08, 2014 at 05:50 PM
Simple do that:
public Vector3 playerPos;
public GameObject yourObject;
void Start(){
playerPos = yourPlayer.transform.position;
instantiate(yourObject, new Vector3(playerPos.transform.position.x + 5, playerPos.transform.position.y, playerPos.transform.position.z), Quaternion.identity);
}
Your answer
