How to shoot a bullet based on another object's position?

I’m new to code, so bear with me. I’ve searched everywhere through google and haven’t found an explanation for this particular implementation.

So I have a character. That character has a disk parented to it. That disk rotates based on the position of the mouse, independent of character movement. There is a “Gun” parented to one point of the target disk, so that the “gun” is always pointing its Z axis directly at the mouse position. On that “Gun” I have a script set to shoot a bullet, which will be any of a number of prefab objects. Each prefab will behave differently (they’ll be different spells), and right now I’m trying to get one to be a fireball-like thing.

It works, it spawns in the correct place but it will only shoot based on its own transform information. If I tell it to travel along Z, it will travel along what is basically world Z. I need it to travel along the Z axis of the “Gun” object, based on wherever that is at the time of instantiation. How do I call up the gun’s z axis information within the fireball prefab? I’m coding in Java, for the record.

There are two ways to approach your problem if you are aiming your projectile. You either have your objects cast in the direction the ‘gun’ is facing, or you have your object cast in the direction of some object in the scene. If you are going to do the former, then change your Instantiate() to:

 var instanceBullet = Instantiate(SimpleBullet, transform.position, transform.rotation);

If what you are going to do is use a target object, then you can do:

 var instanceBullet = Instantiate(SimpleBullet, transform.position, Quaternion.identity);
 instanceBullet.rotation = Quaternion.LookRotation(target.position - transform.position);

There is one other choice. That is to have your cast object not face the target (or decide on their own how to face). If so, you are going to have to somehow communicate to the projectile either 1) a direction or 2) a world position of the target (or even a reference to the target itself). Communicating the actual target to the projectile is the most versatile solution. Then the projectile can figure out its own movements based on either of these two values. The specifics of that movement will depend on the nature of the projectile. For example a homing missile will behave differently than an arrow.