Missle spawn at wrong point

I have seen this discussed of a few threads but every time I see it discussed the user says that they fixed the issue and did not say what they did

Right now i have a simple missile launcher Using basically the same script in the FPS tutorial

using UnityEngine;
using System.Collections;

public class nadeLaunch : MonoBehaviour
{

    public Rigidbody nadePrefab;
    public static float lforce = 20.0f;
    Vector3 shot = new Vector3(0.0f,0.0f,lforce);
    void Update()
    {
        if (Input.GetButtonDown("Fire1"))
        {
            Rigidbody nade = (Rigidbody)Instantiate(nadePrefab,transform.position, transform.rotation);
			
            nade.velocity=transform.TransformDirection(shot);
            //Physics.IgnoreCollision(nade.collider, transform.root.collider);

        }

    }
}

This spawns the object but it it to the extreme left of the controller, This can be corrected my moving the spawn point to the extreme right but that seems more of a bandage than a fix.

My only guess is that the world cord and the launcher cords are different and maybe its using the world cords.

Any Ideas?

Is the spawn GO a child of the gun?

Yes and the gun is a child of the Main camera

not sure if I’m guessing right but is it something like this in the heirarchy

Bullet holder is an empty GO where my shot instantiate is attached to.
If it’s that way then I have no idea why it’s not working

195332--6961--$fps_500.jpg

Yep that is how it is

after rotating the camera 90 deg and rotating it back to 0 not the objects are spawned from behind here is a pic

try this one, gun mesh not mine, was uploaded here long time ago

195358–6963–$target_189.unitypackage (776 KB)

but it didn’t work… :sweat_smile:

I would first comment out the velocity line and see where your instant appears without it as it could be that.

The position of your instant creation is set to the gameobject that the script is running from, if you have a spawn point in mind (front of gun?) then create an empty gameObject there and use the transform.position of that and see what happens.

I dont know c# so imagine its something like this…

// Attach an empty gameobject to this through the inspector

public Rigidbody nadeSpawnObj;
   Rigidbody nade = (Rigidbody)Instantiate(nadePrefab,nadeSpawnObj.transform.position, nadeSpawnObj.transform.rotation);

Hope that helps.

EDIT: Just noticed that you have a spawn point at the front of the gun. So I guess the script is attaced to that rather than something else yes? If so then ignore my second para of info.

Geoff

Whenever I’ve experienced something like this its because there is an animation component on the projectile, so as soon as it spawns its moved to wherever the animation has recorded its position. See if your projectile has an animation component and try removing it if so.

HTH
AC

Thank you for the ideas, there was an animation component with now animation which i removed with, and i set velocity to Vector3.zero both neither effected the position of when the object spawned :?

There is got to be some little thing that i am missing…mabe the camera’s position in world space is different than in local?

What do yall think

If its a child yes. :wink:

Ok I have tried resetting everything in the scene as far as character controller and camera back to 0 but still spawns in wrong place…think it might be the way i made gun or the projectile or something?

(the object swans exactly 20.517 behind the gun)

The other thing in noticed for the forward velocity i have to use in the x direction not the z to go forward so maybe a bunch of stuff is off…

Ok I have finally found what was going wrong after I posted my last comment.

In Maya the projectile and the launcher were originally made in the same scene and the projectile moved (so the projectile would better match the launcher) when I moved it into the new scene it was slightly off 0,0,0 actually about 2.5 in the x range and it was turned facing x instead of z.

After rotating it 90 deg (towards z) and moving it to center it worked correctly.

Thank You everyone for all your help i hope this helps some one else having the same issue

Yup. I would guess it is how you modeled it. The center of your projectile should be set at the origin in your modeling app. And the forward direction on the model in the app should line up with the app’s z-axis (although you can adjust the rotation, I think, when exporting).