In my 2d game I have a character walking around with arrow keys and a weapon as a child of my character GameObject. My weapon has an empty GameObject as child which represents the point where the bullets should be instantiated. My character allways rotates toward my mouse.

But now when I instantiate a bullet at the transform.position of my Empty…

(Instantiate(bullet, firePoint.transform.position, character.transform.rotation * Quaternion(0,0,90,-90)))

… the bullets allways spawn at the position which should be relative to my weapons position:

How can I instantiate my bullets at the position of my FirePoint in the scene view?

edit(moved script from comment)

projectile script:

 #pragma strict
 
 var speed : float;
 
 function Start () {
         var sp = Camera.main.WorldToScreenPoint(transform.position);
         var dir = (Input.mousePosition - sp).normalized;
         var rig = GetComponent(Rigidbody2D);
         rig.AddForce(dir * speed);
 }
 
 function Update () {
 }
 
 
 function OnTriggerEnter2D(other : Collider2D) {
     if (other.gameObject.name == "enemy") {
         Destroy(other.gameObject);
     }
     if (other.gameObject.name == "Player") {
         Destroy(this.gameObject);
     }
 }

Hi JanVog

Your code doesn’t look obviously wrong to me, though I’m not entirely convinced of the behaviour of ‘Instantiate’. Can I suggest you try:

GameObject new_bullet = (GameObject)Instantiate(bullet);
new_bullet.transform.position = firePoint.transform.position;

I deliberately left the rotation bit off there cos it looks a little dodgy ([0,0,90,-90] is not a valid quaternion) but you can add it afterwards if you need to in the same way I did the position. It may even be the case that providing instantiate with an invalid rotation is causing problems.

The code above is slightly different, as I am now instantiating and then setting the position, so the ‘Start’ function of your bullet will get called before the position is set (I think). I’m fairly sure your code should work though, so I mainly present this as a test - it should definitely work. So if it doesn’t, I’d suggest either you are inadvertently setting the bullet’s position elsewhere, or firePoint.transform.position is not what you think it is for some reason.

I would also try adding ‘Debug.Log(firePoint.transform.position)’ just to make sure it is what you think it is. Similarly, if you have a script on your bullet, you could try printing out its own position in the ‘start’ function and every frame in the ‘update’ function to see if it changes in some way you don’t expect.

Try this

one of many found via Google/Search: Unity instantiate transform child

First Comment contains solution.

Since it seems you get two different position value for the seemingly same object the only plausible answer is: Your “firePoint” variable doesn’t reference the actual “firePoint” gameobject you have as child on your weapon in your scene.

Since the name of your weapon has the “(clone)” suffix i guess you instantiated the weapon as well at runtime. Where is the script attached that executes the bullet instantiate line? Is it on the instantiated weapon or on the player? If it’s on the player, how do you initialize your firePoint variable? If you dragged the prefab on to that variable then it’s still referencing the prefab which probably has it’s default position at (0,0,0).

At runtime, select the object in the scene that holds the script which has the “firePoint” variable. If the variable is a public variable you can simply left click onto the object field and Unity will “flash” the actual object in the hierarchy or project view. If the variable is private you could switch the inspector to “debug mode” and you should see the variable.

If firePoint is no member variable at all but just a local variable inside a method you should check the way you obtain the reference to the firePoint object.

ps:
It’s not really a good design when the bullet reads the users input itself and determines the direction itself. The direction in which the bullet should fly should be determined by the weapon that launches the projectile.