Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
avatar image
0
Question by xXSilverswordXx · Jan 30, 2017 at 10:09 PM · 3dshootingprojectile

Top Down Projectiles

As part of my major work, i am trying to create a top down shooting game. The game i am trying to create was originally created in scratch (i know, sorry.) (Original game here so you can see something similar to what i am trying to do: https://scratch.mit.edu/projects/17929257/)

Psuedocode: (Sort of)

 When key space pressed
     create a clone of object "projectile"
     point clone of "projectile" at mouse pointer
     move projectile in the direction it is facing at speed X
 When clone of projectile is touching object Y
     delete it.

I cant find a tutorial that actually shows me how to do this. An example code or a link to a tutorial that shows just how to do the shooting will be appreciated.

Note the project is in unity 2d.

Thanks!

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by DoWeLikeTurtles · Dec 10, 2018 at 04:40 PM

Hello! Two years too late, but I struggled with this for well over 20 hours as a new coder so I'm sure this might be helpful for someone out there! First things first, the script and directions below are my own so if there's a way to improve them (since I am new to c# and unity) then feel free to point that out! Ok! Now, the meat! First script you will put on your player character to instantiate (or spawn for all intensive purposes) the projectile with correct rotation and positioning and I can explain what all of it is so the new kids will be more able to later! I added an attack time counter bc i thought it'd be useful!

 public GameObject bullet;

 public float attackTime;
 private float attackTimeCounter;


 private bool attacking;

 // Use this for initialization
 void Start()
 {

 }

 void Update()
 {
 //this is a simple bool to check if the attacktimecounter is at 0 or less yet! you can safely remove this part if you want no timer!
     if (!attacking)
     {
 //you can change this to whatever key you want it to be and if you want to have to click each time you want to shoot, you can change it to GetKeyDown or GetKeyUp!
     if (Input.GetKey(KeyCode.Mouse0))
         {
             attacking = true;
             attackTimeCounter = attackTime;

             Vector2 target = Camera.main.ScreenToWorldPoint(new Vector2(Input.mousePosition.x, Input.mousePosition.y));
             Vector2 myPos = new Vector2(transform.position.x, transform.position.y);
             Vector2 direction = target - myPos;
             direction.Normalize();
             Quaternion rotation = Quaternion.Euler(0, 0, Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg + -90f);
             GameObject projectile = (GameObject)Instantiate(bullet, myPos, rotation);
         }
     }

 //this is the attack time counter which you can set in unity
     if (attackTimeCounter >= 0)
     {
         attackTimeCounter -= Time.deltaTime;
     }

     if (attackTimeCounter < 0)
     {
         attacking = false;
     }
 }


Secondly, you'll want to make a Bullet prefab! Create a new 2d sprite with a rigidbody2d and a collider of sorts (what type I guess depends on the shape of your projectile!) Make sure the rigidbody2d gravity is set to "0" and the collider is set to "is trigger" then create a new script called BulletMove! (Why that? idk, it fits well) And in this script will be the following:

 public float timeToDestroy;

 public float amount;
 private Rigidbody2D myRigidBody;

 //beautiful work of art by me
 private bool isMoving;

 void Start()
 {
     myRigidBody = GetComponent<Rigidbody2D>();
 }

 // Use this for initialization
 void Update()
 { 

     if (Input.GetKey(KeyCode.Mouse0))
     {

 //this is a simple bool to check if the bullet is in motion! simply put, once given motion, the bool will be set to true prohibiting another shot to alter the position of the previous bullet! if you want to learn, go ahead and remove this part and see what happens!
         if (!isMoving)
         {
             Vector3 sp = Camera.main.WorldToScreenPoint(transform.position);
             Vector3 direction = (Input.mousePosition - sp).normalized;
             myRigidBody.velocity = direction * amount;

             //problem solver bool right here! took me lots of hours to get it figured out!
             isMoving = true;
         } 
     }


         //destroy this object over time set by you in unity
         timeToDestroy -= Time.deltaTime;
         if (timeToDestroy <= 0)
         {
             Destroy(gameObject);
         }
 }
 // this is what makes the bullet disappear when it encounters a collider with the tag of Enemy or collider! The capitalization is important when making triggers!
 private void OnTriggerEnter2D(Collider2D other)
 {
     if (other.gameObject.tag == "Enemy")
     {
         Destroy(this.gameObject);
     }

     if (other.gameObject.tag == "collider")
     {
         Destroy(this.gameObject);
     }            
 }


Next, you'll want to add the sprite to your prefabs and delete the one on the map you just made! But it will be saved in prefabs! Now, on your players bullet creation script you will want to drag and drop the bullet prefab onto the box to the right of "bullet" so it knows what to spawn each time! then you'll of course want to set your attack time right under that (I use 0.40 but I guess its up to how you wanna do it!) then go on over to your bullet prefab and set up your TimeToDestroy (this is in seconds, not distance) and your projectiles movespeed (located to the right of "Amount" and that should make you able to fire in the direction of the mouse! Now, in order to get the "disappears on collision" part working you'll have to set up the tags with colliders all by yourself! Thanks for joining in!

-Turtles

Comment
Add comment · Show 2 · 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 DoWeLikeTurtles · Dec 10, 2018 at 04:41 PM 0
Share

make sure to upvote so others can see later! That way newer creators wont have to struggle so much!

avatar image DoWeLikeTurtles · Dec 10, 2018 at 04:51 PM 0
Share

Now that I look at it, you can remove most of a line of code to not get errors!

          GameObject projectile = (GameObject)Instantiate(bullet, myPos, rotation);

should look like

          Instantiate(bullet, myPos, rotation);

using the previous version just clutters the code!

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.

Follow this Question

Answers Answers and Comments

95 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

Related Questions

How To Get Player Arms To Be On The Same Up Down Coordinate As The Crosshair 0 Answers

instantiating object from camera position onto touch position 1 Answer

Shooting at mouse position with a slight camera rotation 0 Answers

weapon Spread 0 Answers

Can't make my AI shoot projectiles with raycast 0 Answers


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