- Home /
Instantiating objects at position
I'm trying to instantiate an object in front of the player. For some reason, it instantiates along the x and y values correctly, but it doesn't take into account the player's rotation, causing it to end up along the global z rather than the local with this:
theObject : GameObject;
function Update() {
if (Input.GetKeyDown(KeyCode.E))
Instantiate(theObject, Vector3(transform.position.x, transform.position.y, transform.localPosition.z + 1), transform.rotation);
}
Answer by Eric5h5 · Jan 01, 2013 at 12:35 AM
localPosition is only relevant if the object is a child of another, since it's the local position of the child compared to the parent. Use transform.forward.
Instantiate (theObject, transform.position + transform.forward * 1.0, transform.rotation);
Replace 1.0 with the desired distance.
Just curious, I know this is correct, but why are you multiplying transform.forward by 1.0? Wouldn't transform.forward be the same as transform.forward * 1? Good answer and no intent to offend, just wondering if I'm missing something.
Yeah, you're right. I think he was just pointing out where to put the distance.
As I said in the answer, "replace 1.0 with the desired distance". ;) If the desired distance is 1.0, then of course you wouldn't need it.
Your answer
Follow this Question
Related Questions
I need help with the location of instatiated objects on the scene and correct interaction with this 2 Answers
Add vector according to objects's local position & roation 1 Answer
Instantiate VS placement in editor 0 Answers
Randomly position Instantiated GameObject's 1 Answer
Dropping an object behind player in respect to rotation,How to drop an object behind the player 1 Answer