Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Quags · Aug 27, 2013 at 03:46 AM · c#2dshooting

need help with 2d shooting

so i got the movement down but i need some help with the shooting. ive been messing around with this but i cant seem to figure it out. im trying to have it so it shoots with the w,a,s,d keys.im using C# here is my code:

 using UnityEngine;
 using System.Collections;
 
 public class Creator : MonoBehaviour {
 
     public Rigidbody ThePrefab;
     private float fireRate = .15f;
     private float nextFire = 0;
     private float bulletSpeed = 500f;
     
     // Use this for initialization
     void Start () {
             
     }
     
     // Update is called once per frame
     void Update () {
 
         //shoot up
         if (Input.GetKeyDown(KeyCode.W) && Time.time > nextFire){
                nextFire = Time.time + fireRate;
                Debug.Log ("nf" + nextFire);
                Rigidbody instance;
                instance = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
               instance.AddForce(transform.up * bulletSpeed);
         }
         
         //shoot left
           if (Input.GetKeyDown(KeyCode.A) && Time.time > nextFire){
                nextFire = Time.time + fireRate;
                Rigidbody instance;
                instance = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
                instance.transform.rotation = Quaternion.AngleAxis(-90, Vector3.forward);
                instance.AddForce(transform.right * bulletSpeed);
         }
         
         //shoot down
          if (Input.GetKeyDown(KeyCode.S) && Time.time > nextFire){
                nextFire = Time.time + fireRate;
                Rigidbody instance;
               instance = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
                instance.transform.rotation = Quaternion.AngleAxis(180, Vector3.right);
                instance.AddForce(transform.up * -bulletSpeed);
           }
         
           //shoot right
          if (Input.GetKeyDown(KeyCode.D) && Time.time > nextFire){
                nextFire = Time.time + fireRate;
                Rigidbody instance;
                instance = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
                instance.transform.rotation = Quaternion.AngleAxis(90, Vector3.forward);
                instance.AddForce(transform.right * -bulletSpeed);
           }
             
                 
         
     }
 }
Comment
Add comment · Show 3
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 robertbu · Aug 27, 2013 at 04:36 PM 0
Share

@Quags - I posted an answer 4 hours ago. But the question is acting strange. It lists the number of answers a '-1' and is still appearing on the unanswered list. Here is a copy of my answer posted in as a comment:

 using UnityEngine;
 using System.Collections;
  
 public class Createor : $$anonymous$$onoBehaviour {
  
     public GameObject ThePrefab;
     private float fireRate = .15f;
     private float nextFire = 0;
     private float bulletSpeed = 500f;
  
     void Update () {
        GameObject instance;
  
        //shoot up
        if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.W) && Time.time > nextFire){
            nextFire = Time.time + fireRate;
            Debug.Log ("nf" + nextFire);
            instance = Instantiate(ThePrefab, transform.position, transform.rotation) as GameObject;
            instance.rigidbody.AddForce(transform.up * bulletSpeed);
        }
  
        //shoot left
          if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.A) && Time.time > nextFire){
            nextFire = Time.time + fireRate;
            instance = Instantiate(ThePrefab, transform.position, transform.rotation) as GameObject;
            instance.transform.rotation = Quaternion.AngleAxis(-90, Vector3.forward);
            instance.rigidbody.AddForce(transform.right * -bulletSpeed);
        }
  
        //shoot down
         if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.S) && Time.time > nextFire){
            nextFire = Time.time + fireRate;
            instance = Instantiate(ThePrefab, transform.position, transform.rotation) as GameObject;
            instance.transform.rotation = Quaternion.AngleAxis(180, Vector3.right);
            instance.rigidbody.AddForce(transform.up * -bulletSpeed);
      }
  
      //shoot right
         if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.D) && Time.time > nextFire){
            nextFire = Time.time + fireRate;
            instance = Instantiate(ThePrefab, transform.position, transform.rotation) as GameObject;
            instance.transform.rotation = Quaternion.AngleAxis(90, Vector3.forward);
            instance.rigidbody.AddForce(transform.right * bulletSpeed);
        }
     }
 }
avatar image ArkaneX · Aug 27, 2013 at 04:51 PM 0
Share

@robertbu - about half an hour ago I changed @Quags' answer to this question to comment in moderation queue. I got 'page does not exist' error and entry is still visible for me in moderation, but now only with 'send message' option.

At the time I was doing it, your answer wasn't visible for me. I don't have a clue why... $$anonymous$$oreover - now I see 'show 2 more replies' under your comment, but clicking on the link reveals nothing.

This looks like some QATO bug.

avatar image Quags · Aug 27, 2013 at 06:07 PM 0
Share

ok yeah i got this all fixed now thanks

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Aug 27, 2013 at 12:28 PM

Here's a quick edit:

 using UnityEngine;
 using System.Collections;
  
 public class Createor : MonoBehaviour {
  
     public GameObject ThePrefab;
     private float fireRate = .15f;
     private float nextFire = 0;
     private float bulletSpeed = 500f;
  
     void Update () {
         GameObject instance;
 
        //shoot up
        if (Input.GetKeyDown(KeyCode.W) && Time.time > nextFire){
            nextFire = Time.time + fireRate;
            Debug.Log ("nf" + nextFire);
            instance = Instantiate(ThePrefab, transform.position, transform.rotation) as GameObject;
            instance.rigidbody.AddForce(transform.up * bulletSpeed);
        }
  
        //shoot left
          if (Input.GetKeyDown(KeyCode.A) && Time.time > nextFire){
            nextFire = Time.time + fireRate;
            instance = Instantiate(ThePrefab, transform.position, transform.rotation) as GameObject;
            instance.transform.rotation = Quaternion.AngleAxis(-90, Vector3.forward);
            instance.rigidbody.AddForce(transform.right * -bulletSpeed);
        }
  
        //shoot down
         if (Input.GetKeyDown(KeyCode.S) && Time.time > nextFire){
            nextFire = Time.time + fireRate;
            instance = Instantiate(ThePrefab, transform.position, transform.rotation) as GameObject;
            instance.transform.rotation = Quaternion.AngleAxis(180, Vector3.right);
            instance.rigidbody.AddForce(transform.up * -bulletSpeed);
      }
  
      //shoot right
         if (Input.GetKeyDown(KeyCode.D) && Time.time > nextFire){
            nextFire = Time.time + fireRate;
            instance = Instantiate(ThePrefab, transform.position, transform.rotation) as GameObject;
            instance.transform.rotation = Quaternion.AngleAxis(90, Vector3.forward);
            instance.rigidbody.AddForce(transform.right * bulletSpeed);
        }
     }
 }
Comment
Add comment · 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

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

16 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

Related Questions

RayCast2D direction is always showing origin point(0,0,0) on game and ignores direction Vector2D.(Unity 2017.4.7.f1) 0 Answers

Cinemachine camera rotating with player 2D 1 Answer

Multiple Cars not working 1 Answer

Transform.translate bullets problem 0 Answers

OnTriggerEnter2D with multiple objects 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