• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by AlejandroBoss10 · Mar 22, 2019 at 09:03 PM · shootingplanetopdowninstantiate prefabsimple

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AlejandroBoss10 · Mar 26, 2019 at 06:01 AM 0
Share

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.

1-li-opt.jpg (26.6 kB)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image highpockets · Mar 23, 2019 at 12:19 AM 0
Share

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

avatar image AlejandroBoss10 · Mar 26, 2019 at 05:43 AM 0
Share

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?alt text

ssaasasa.png (11.1 kB)
avatar image highpockets AlejandroBoss10 · Mar 26, 2019 at 06:49 AM 0
Share

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”);
 }

avatar image highpockets highpockets · Mar 26, 2019 at 07:31 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

105 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How Could I Shoot In My Top Down 2D Game 2 Answers

Instantiate rotation is not correct, why? 1 Answer

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges