The instantiated prefab doesn't fire correctly from the player

Hello!

I’ve been breaking my head on fixing this considering I’m pretty sure it’s a very easy fix. (complete beginner here).
I have the FireMissile method instantiating my missile prefab but the issue is that the missile always starts 2f above the transform.position. I’m thinking it has to do with world or local but don’t have any idea how to change that.(I tried changing it to transform.localPosition and transform.localRotation but no dice).

Even when it’s horizontal it shoots from 2f above the middle of the gameObject (so in the middle section, which looks weird).

Any help would be greatly appreciated!

    private void FireMissile()
    { 
        if (Input.GetKeyDown(KeyCode.F))
        {
            Vector3 firePoint = new Vector3(0f, 2f, 0f);
            Instantiate(rocketProjectile, transform.position + firePoint, transform.rotation);
       }
    }

Hello there,

Sorry if I’m misunderstanding this, but if your problem is that the missile get instantiated 2f above the position of the gun, that’s because your code is telling you to do it.

You’re not instantiating the missile at the gun’s position, you’re instantiating it at the gun’s position + firepoint, which is defined just above as having a Y of 2f. Change that 2 to 0, or remove “+ firePoint”, and the missile will then get instantiated at the exact position of the gun.

Also just in case, you might want to check the pivot of the missile. If it isn’t centered, then that offset will affect the position it gets instantiated at. You can do that by clicking on a missile and changing between “center” and “pivot” in the top left corner.

Hope that helps!

Best,

~LegendBacon

For anyone in the future looking:

(thanks to @Legend_Bacon putting me on the right track!)

I fixed it by making a new (empty) GameObject in my scene (which is a child of my player prefab) and creating a public GameObject firePoint in the player script.
I put the empty firePoint object through the inspector on the firepoint reference slot.

I then changed my transform.position and transform.rotation to firePoint.transform.position and firePoint.transform.rotation.

    private void FireMissile()
    { 
        if (Input.GetKeyDown(KeyCode.F))
        {
            Instantiate(rocketProjectile, firePoint.transform.position, firePoint.transform.rotation);
       }
    }

As I’m a beginner, I have no idea if this is a viable way to do this but it worked for me!