[Solved] How do I make Prefabs shoot Prefabs?

It may sound easy, Imagine a Enemy Spawner, and those Enemies, fire a proyectile, its 2D and they can only be shooting Up, Down, Left and Right, but I cant get the shoot to move in the directon they are facing, here’smy code:

[This is the Code for the Enemy]

public class CPUScript : MonoBehaviour
{

    public GameObject CPUShootPrefab; // The projectile prefab
    public float CPUFireRate; //Enemy's Fire Rate
    private float CPUFireDelay; //I'll explain this one later

      void Update()
      {
            if (Time.time > CPUFireDelay)
                   {
                    CPUFireDelay = Time.time + CPUFireRate; //This is a cooldown so it wont shoot every frame
                    GameObject CPUbullet = (GameObject)Instantiate(CPUShootPrefab0, transform.position, Quaternion.identity); //Spawns the "CPUShootPrefab" In the same X, Y and Z
                    CPUProjectiles.Add(CPUbullet);
            }
        }
}

Well, the CPU just spawns the prefab when he detects there’s a player in front, the problem is in the Projectile script or something:

using UnityEngine;
using System.Collections.Generic;

public float MoveSpeed;
    private float MoveX; //I'll Explain this one later
    private float MoveY; //I'll Explain this one later
    private float ZRot; //I'll Explain this one later
    private float Delay = 0.1f; // A Delay
    private float FinalDelay;  // A Delay v2

    void Start()
    {
        FinalDelay = Time.time + Delay;  //Delay v2 is set to current time plus Delay (0.1)
    }
    void Update()
    {
        if (Time.time == FinalDelay)  //When current time is equal to Delay v2 then SetDirection will run
         {   SetDirection();  }

        GetComponent<Rigidbody2D>().velocity = new Vector2(MoveX * MoveSpeed, MoveY * MoveSpeed); //Move Constantly
}

    void OnCollisionEnter2D(Collision2D coll)
    {
            ZRot = coll.transform.rotation.z;  // Sets "ZRot" to the object it collided with's z rotation
            //Note that the prefab is spawned in the cpu, so it will be the first thing it will ever collide with before 0.1 seconds, that is when SetDirection will run and permanently set the Move Value
    }

void SetDirection() //This is run atfer 0.1 seconds the prefab was spawned
    {
        if (ZRot >= 315 || ZRot < 45) //If the Enemy who fired this prefab's rotation was 315 or more, or less than 0
        {                           //Then it must be pointing upwards (cause the Rigidbody isnt constrained in Z rotation and as
                                    //it collides with the cpu when spawned it can rotate slighly, and if I placed == 0 and it rotated to -0.000000000001 it wouldnt be detected)
            MoveX = 0;
            MoveY = 1;
        }
        if (ZRot >= 45 && ZRot < 135)  //Same for facing Right
        {
            MoveX = 1;
            MoveY = 0;
        }
        if (ZRot >= 135 && ZRot < 225)  // Down
        {
            MoveX = 0;
            MoveY = -1;
        }
        if (ZRot >= 225 && ZRot < 315)  //and Left
        {
            MoveX = -1;
            MoveY = 0;
        }
    }
}

But I dont know why, most of the time it shoots perpendicularily, I mean, If it is facing Upwards or Downwards the shoot will move Right or Left, and it its facing Right or Left the shoot will most likely move Up or Down, sometimes it shoots in the correct direction and sometimes in the opposite one, so the collision is being detected, Anobody knows what’s up?

Edit #1:
Oh god lol, they are moving in the direction the player is facing, WHAT!?

[SOLVED] :smile:

third parameter is the rotation of the object, if you want them to face the same way as the transform, don’t use the identity quaternion, use the transform’s rotation.

second script is then mostly redundant, just get the bullet to move “transform.forwards” and it’ll head in the direction it’s facing.

Well, I think I had tried this before, but without “transform.forwards”, lets see

O god lol

I just realised xDDDD

Ol’ Enemy shoot moves towards the direction player is facing xDDD, because it has ol’ player’s projectile script xDDDD

ok, I dont know if I used “tansform.forwards” in the correct way but it didnt worked, instead, apparently, if you use “Debug.Log(”" + transform.rotation.z);" the values for 45, 135, 225 and 315 degrees are the following:
45= 0.3826835
135= 0.9238796
225= -0.9238794
315= -0.3826833

And this worked:

void Start()
    {
        if (transform.rotation.z >= -0.3826833 || transform.rotation.z < 0.3826835)
        {   
            MoveX = 0;
            MoveY = 1;
        }
        if (transform.rotation.z >= 0.3826835 && transform.rotation.z < 0.9238796)
        {
            MoveX = -1;
            MoveY = 0;
        }
        if (transform.rotation.z >= 0.9238796 && transform.rotation.z < -0.9238794)
        {
            MoveX = 0;
            MoveY = -1;
        }
        if (transform.rotation.z >= -0.9238794 && transform.rotation.z < -0.3826833)
        {
            MoveX = 1;
            MoveY = 0;
        }
    }