- Home /
Spawning a prefab X units in 1 direction from an object
Hi,
I'm trying to spawn a prefab explosion at the end of a cannon when i click.
I have:
void OnMouseUp () {
Instantiate(BlueExplosionPrefab, transform.position, Quaternion.identity);
}
This of course spawns the explosion at the origin of the Cannon. How do I make it do something like spawn the explosion at the origin+X units on a given axis?
Thank You.
why not just put an empty game object at the spot you want it to spawn at, and instantiate it there?
I honestly don't even know how to get that position. I've been using unity for about 3 days now and I've never programmed anything ha. I'll try to look that up.
O$$anonymous$$! I got it thanks! I had previously tried something like that but in my noob fashion did transform.position.spawnpoint ins$$anonymous$$d of spawnpoint.transform.position
Answer by BiG · Apr 30, 2012 at 08:28 PM
Follow Lo0NuhtiK's advice, in his comment, or, if you want to mantain your method, do the following:
pos = transform.position;
Instantiate(BlueExplosionPrefab, Vector3(pos.x + 5, pos.y ,pos.z), Quaternion.identity);
This will instantiate the explosion in the same point as the origin, but with an offset of 5 units on the X axis.
Ahah! Thanks Lo0Nuthi$$anonymous$$! ;)
PS: Glad to see you again here...^_^
Answer by stingman · Apr 30, 2012 at 08:29 PM
You can always do what LoONuhtiK said but to answer your question in the way you want to do it... just add a Vector3 addition to your code:
Instantiate(BlueExplosionPrefab, transform.position + Vector3(10,0,0), Quaternion.identity);
That's the simplest explanation. That will instantiate the prefab 10 units to the right on the X axis. Make sense? Just alter the code to fit your needs.
Answer by Seth-Bergman · Apr 30, 2012 at 08:30 PM
like loonuhtik says, you could just place an empty object exactly where you want the explosion, that would be the easiest way:
var explosion : Transform;
void OnMouseUp () {
Instantiate(BlueExplosionPrefab, explosion.position, Quaternion.identity);
}
or, to answer the original question, you would say:
void OnMouseUp () {
var newPosition = transform.position; newPosition.x += (whatever number here); Instantiate(BlueExplosionPrefab, newPosition, Quaternion.identity); }