Instantiating an object from a moving object (with bullet trail)

Hello!
I’m working on a star fox type game and am having trouble with the instantiation of bullet from a ship while it is moving.

essentially there is a bullet spawn point on the front of the ship, when I click ‘mouse 0’ a new game object (bullet) is instantiated from that position. That gameobject has a trail attached to it.giving it a laser gun type effect. This works great when ship is not moving. As soon as I move anywhere on the X, Y, Z axis the bullet appears as if it is being fired from where the ship was, instead of where the ship is.

So in my BulletController I have this:

private void Update()
    {
        _timer += Time.deltaTime;

        if (_timer >= _duration)
        {
            Destroy(gameObject);
        }
    }

    void FixedUpdate()
    {
        transform.position += transform.forward * speed * Time.deltaTime;
    }

In my GunController I have this:

public void Shoot()
    {
        if (_lastShootTime + _shootDelay < Time.time)
        {
            ShootingSytem.Play();

            GameObject InGameBullet = Instantiate(Bullet, BulletSpawnPoint.transform.position, BulletSpawnPoint.transform.rotation);

            _lastShootTime = Time.time;
        }
    }

    void Update()
    {
        if (Input.GetKey("mouse 0"))
        {
            _shouldShoot = true;
        }
    }

    void FixedUpdate()
    {
        if (_shouldShoot)
        {
            Shoot();
            _shouldShoot = false;
        }
    }

Is there a way to kind of account for the velocity of the BulletSpawnPoint or something so that it shows that it is shooting from the gun?

Add the velocity of the ship to the bullet velocity when being fired. Simply pass the velocity of the ship and cache that to use in the position updates. That’s also how it works in real life.

transform.position += (transform.forward * speed * Time.deltaTime) + shipVelocity;

so literally something like this?

Yep

Didn’t seem to work. I accessed the velocity via the rigid body and added it as shown above. bullet still appears to spawn behind me when I’m flying forward. Been bashing my head against the keyboard for hours on this.

Debug.log the spawn position, gun position, and velocities

I see the words “physics” and “transform” being used in the same sentence and I always get worried.

This might well be one of those times where understanding that the Rigidbody is the authority on the pose (position and rotation). You should always refer to it when wanting to know where it is. Setting a Transform doesn’t immediately change anything in Unity, not even rendereres. They only read it when they render which is per-frame so you don’t notice it. Physics is the same but by default it runs during the fixed update so the Rigidbodywon’t be at that position. This is why you should not drive physics stuff via the Transform, use the Rigidbody API.

An example would be interpolation; the Transform will be moving from the last body position to the current one. If you read the Transform.position as to where to instantiate or position something then it’ll not be where the body is at; it’ll be the historic movement.

It’s unclear how this projectile is moving too. If it’s physics, you should be setting the body. If you instantitate it at the Rigidbody position of the player which is ahead of any current interpolation position then when the physics next runs, the projectile will start moving from the correct position.

The above might not be related to your problem but it is something to be aware of.