How do I make a child object move from the parent's rotation?

Hi!
I’m trying to make a script for a projectile launcher. I have a script that is for creating the projectile and a simple script for the movement of the projectile.

This creates the projectile:

void Update () {
if (Input.GetKeyDown(“z”))
{
Instantiate(Magicbolt, new Vector3(0, 0, 1f), Quaternion.AngleAxis(90, Vector3.up));
}
}

The movement is a simple transform.position by Vector3.
Now what I want is to make the projectile move in the direction the parent object is facing. I’d guess I should use something else than a vector so as not to set the direction or add something that uses the rotation of the parent.

Hi!

I will give you an alternative to handle this problem. Create a script in the parent (i will call it main character) that is responsible for creating a new projectile. It would be something like this

public GameObject projectile;
GameObject thisProjectile;

void Update(){
        Input.GetKeyDown(KeyCode.Z){
              thisProjectile = Instantiate(Magicbolt, transform.position, transform.rotation) As GameObject;
              thisProjectile.setActive(true);
        }
}

Then in the projectile prefab you uncheck the GameObject so it stays disable and when you press Z the main character spawns the project in the position that you want (in this code the main character position itself) and with the same angle as the main character, and activate it. Then in the projectile gameobject you create a script where you set it velocity and other things you may want in the Start function. There’s plenty other ways to solve this, buy i think in this way you have good portability in your script.

Good Luck!

There is a very simple approach to this. You can set the projectile’s orientation to match whatever launches it.

Before I jump into this, I’m going to assume two things. First, I’m going to assume that the position to instantiate the projectile at is relative to the object launching it. Second, I’m going to assume that the rotation applied is necessary to correct its orientation to properly face forward. (More specifically, I’m assuming what you’ve already put together is correct for what you’ve been doing under the circumstances)

Now, on to actually making this work:

Matching rotation:

public Vector3 magicBoltOffset;
public Vector3 rotationOffset;
// ...

// Reorient spawn position offset relative to launcher's orientation (assuming launcher faces forward)
Vector3 finalBoltOffset = new Vector3(launcher.transform.right * magicBoltOffset.x, launcher.transform.up * magicBoltOffset.y, launcher.transform.forward * magicBoltOffset.z);

Instantiate(Magicbolt, launcher.transform.position + finalBoltOffset, launcher.transform.rotation * Quaternion.Euler(rotationOffset));

// ...

Then, give your projectile(s) a script to have them move themselves using transform.forward as their intended direction.