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.

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.

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.

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);
}