- Home /
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.
I want it to shoot like a regular plane similar to those in WW2. Where if you press down the J key it shoots say a bullet every .2 seconds continuously. Think of a WW2 plane shooting but in top down mode. That's what I'm trying to accomplish. I want the plane to shoot in whatever direction it's facing.
Answer by highpockets · Mar 23, 2019 at 12:09 AM
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.
I just made an edit to the coroutine code. I added a parameter (shotDir) which takes the forward direction vector of the character and passes it to the function. The way it was previously would have made the bullet change direction if the character rotated while it was shooting, but this will keep the same direction for the duration of the shot. Cheers
Where is it that I'm supposed to put the " Vector3 directionToShoot = transform.forward;"? I got everything else to work but I get an error. This is what I get.
A field initializer cannot reference the nonstatic field, method, or property `UnityEngine.Component.transform'
I believe I'm just putting it in the wrong location but I'm not sure. I put it up near the top.
Also, the second part for the shooting, do i put that on the player or the shot itself?
The directionToShoot assignment should be right after the conditional for the $$anonymous$$eyCode.J has tested true. Because you need to get the direction when you know the player has the J key down.
This whole script could be on the player. But you will need to have a reference to the bullet, at the beginning for example:
GameObject cubeBullet;
void Start(){
cubeBullet = GameObject.find(“CubeBulletName”);
}
If you can’t figure it out, post the whole script after you have tried to implement the changes and I’ll take a look if you want.
Your answer
![](https://koobas.hobune.stream/wayback/20220612194108im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How Could I Shoot In My Top Down 2D Game 2 Answers
I need some help with camera rotations 0 Answers
2D Top Down Shooter Shooting Problems 1 Answer
How to make Enemy shoot at Player Top Down 1 Answer
Shooting when cklicking on UI. 0 Answers