How to shoot Top Down Plane 2D?

Hey there everyone, thanks for helping me out. So here’s what I’m trying to accomplish. I have a plane from the top down perspective and I want it to be able to shoot forward in the direction that it’s looking. I control the plane with the ‘WASD’ keys, and I want to be able to shoot with ‘J’. I know that I would have to instantiate some sort of object/prefab but I don’t know how to do that and make it move the way that the player/plane is facing.

Basically what I want is whatever direction the player is facing, that’s the direction that the bullets will go. Also if I hold down the ‘J’ key, it will continue firing.

I’m sure this is easy to accomplish but I have no idea how. Here’s my movement script if that’s easier for you. Thank you



using System.Collections;
using UnityEngine;

public class SimpleMoveScript : MonoBehaviour
{
public float NormalSpeed;
public float ExtraSpeed;
public float TurnSpeed;

void Update()
{
    transform.Translate(0 * Input.GetAxis("Horizontal") * Time.deltaTime,
    NormalSpeed * Input.GetAxis("Vertical") * Time.deltaTime, 0f);

    if (Input.GetKeyDown(KeyCode.LeftShift))
    {
        NormalSpeed += ExtraSpeed;
    }
    else if (Input.GetKeyUp(KeyCode.LeftShift))
    {
        NormalSpeed -= ExtraSpeed;
    }

    if (Input.GetKey(KeyCode.D))
    {
        transform.Rotate(new Vector3(0, 0, TurnSpeed));
    }

    if (Input.GetKey(KeyCode.A))
    {
        transform.Rotate(new Vector3(0, 0, -TurnSpeed));
    }
}

}

If you need me to be clearer I’ll try. Thank you. Please write any code in C# please, thank you.

Ok, first I think you should get the forward direction vector of your character transform:

Vector3 directionToShoot = transform.forward;

transform.forward should give you that direction unless your model’s forward vector does not align with the positive world z axis when it has zero rotation. But if it doesn’t align, just select the character transform and the axis gizmo will show you what is your character’s forward direction vector.

Now you need something to shoot. You can instantiate or you can just place some objects behind the scenes, this sabes processor speed. Let’s just say you want to shoot little cube bullets. Take a cube and place it in your scene, call it CubeBullet.

Now inside your script, you can do the following:

public Transform cubeBullet;

Now in the inspector drag the CubeBullet object into the public field you just created in the script. Now code your bullet behaviour:

bool isShooting = false;

void Update(){

if (Input.GetKeyDown(KeyCode.J) && !isShooting)
{
isShooting = true;
directionToShoot = transform.forward;
cubeBullet.position = transform.position;
StartCoroutine(LetsShoot(directionToShoot));
}
}

iEnumerator LetsShoot(Vector3 shotDir){

while(Vector3.Distance(cubeBullet.position, transform.position) < 100){
cubeBullet.Translate(shotDir * Time.deltaTime);
yield return new WaitForEndOfFrame();
}
isShooting = false;
//hide bullet
cubeBullet.position = new Vector3(5000,5000,5000);
    }

The above checks if you have pressed J and if so and you are not already shooting, it starts a coroutine which shoots a bullet and it doesn’t finish until the bullet has reached 100 units from the character. Then we hide the bullet way out in the void and then you can shoot again by pressing J. If you want to add more bullets, it’s of course the same concept, but you can then use an object pool. Or you can instantiate if you prefer.

Hope this helps.