How do I instantiate an object at the location of another object

My question basically is: How do I instantiate an object at the location of another object, but that second Object has no script attached to it.

About my Project:
I basically want to spawn a Bullet (2D Jump’n Run made in 3D) in front of the player(a Cube :P), but the player is holding a gun (parented to player) which is not just in front of the player but also (on the Z axis ~+0,25) a bit left and the Barrel (spawn point ) is (world Y axis ~+0,25) a bit below the players middle.

I could also spawn the projectile relative to the player, but I don’t know how to do that either.

Oh and I want to do this in Javascript since I don’t know C# that well.

If you know other ways to solve my problem: they’re much appreciated.

The Unity Scripting Reference and Google didn’t help and I’m a beginner.


Firstly… you will learn to do all these things in a fun and interesting way, if you find the Tornado Twins Unity Tutorial, episodes 0 to 10, it is what i leaned to do all these things… you can skip half the tutorial, but get to grips with size, position, innstantiate, codes that they do.

Ok. in javascript, you can have various variables… the target object you want to position at:

var targetObj : GameObject;
var targetObjPos : Vector3; //x y z position numbers

var newObject = Instantiate(BulletPrefab,targetObjPos,Quaternion.identity); = "newbullet";

Drudiverse is correct but I kind of want to write the code a bit more clearly since you said you’re just beginning.

//When you save the script after you enter these variables, you will
//get two new boxes. Drag and drop the target object in to the
//targetObj field and the bullet you want to spawn in the
//bulletPrefab field.
var targetObj : GameObject;
var bulletPrefab : GameObject;

function Update () 
	//Here you can use whatever method you would like to instantiate the bullet.
	//For example, we can use the "Fire1" button to spawn the bullet.
		//Instantiate works by spawning the object of choice, then where it will be positioned and at what rotation.
		var newBullet = Instantiate(bulletPrefab, targetObj.transform.position, Quaternion.identity);
									//object	  //position					//rotation

I would just like to point out that you wish for the object to spawn at the same location AND rotation you should use the following line of code:

newObject= Instantiate(newObject, targetObj.transform.position, targetObj.transform.rotation);

Using Quaternion.identity will give you the identity rotation (no rotation) while targetObj.transform.rotation will give you the current rotation of that game object.

This can be useful if you want projectiles to point in the right direction when firing out of a gun, for example.

All the best,

Cranky Franky