• 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 andreyazbyn · Sep 06, 2015 at 02:09 PM · 2draycastshooter2d-sidescroller

2D sidescroller shooting

I made the arm rotate using wasd using this code:

 public class ArmRotation : MonoBehaviour {
 
     public int armRot = 0;
     // Use this for initialization
     
     
     // Update is called once per frame
     void Update () {
         if(Input.GetButton("AimUp")&&Input.GetButton("AimSide"))
             armRot = 45;
 
         else if (Input.GetButton("AimDown"))
             armRot = -45;
 
 
         else if (Input.GetButton("AimUp"))
             armRot = 90;
             
          else armRot = 0;
 
         transform.rotation = Quaternion.Euler(0f,0f, armRot );
     }
 }
 

but i can't figure out how to make my gun shoot properly i tried this but it doesn't work :

public class WeaponScript : MonoBehaviour {

     public float fireRate = 0;
     public float Damage = 10;
     public LayerMask notToHit;
 
     float timeToFire = 0;
     Transform firePoint;
     Transform firePoint2;
 
     // Use this for initialization
     void Awake () {
         firePoint = transform.FindChild ("FirePoint");
         if (firePoint == null){
             Debug.LogError ("FirePoint not found");
         }
         firePoint2 = transform.FindChild ("FirePoint2");
         if (firePoint == null){
             Debug.LogError ("FirePoint2 not found");
         }
     }
     
     // Update is called once per frame
     void Update () {
         if (fireRate == 0){
             if (Input.GetButtonDown("Fire")){
                 Shoot();
             }
         }
         else {
             if (Input.GetButtonDown("Fire")&& Time.time > timeToFire){
                 timeToFire = Time.time + 1/fireRate;
                 Shoot();
 
             }
         }
     }
     void Shoot (){
     
         Vector2 firePointPosition = new Vector2 (firePoint.position.x, firePoint.position.y);
         Vector2 firePointPosition2 = new Vector2 (firePoint2.position.x, firePoint2.position.y);
         Debug.DrawLine (firePointPosition, firePointPosition2, Color.red);
         RaycastHit2D hit = Physics2D.Raycast(firePointPosition, firePointPosition2- firePointPosition);
 
         
 
     }
     }
     

where firePoint is a empty game object next to the barrel and firePoint2 is a empty game object located in front of the barrel (by 0.3 units)

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
Best Answer

Answer by Eudaimonium · Sep 06, 2015 at 09:14 PM

The logic for determining if you can shoot in your Update method is... ehh, FUBAR.

Basically if you just want to have a delay between firing, simply use this:

 public float RefireDelay = 1f;
 private float _refireDelay;
 
 public void Start()
 {
    _refireDelay = RefireDelay;
 }
 
 public void Update()
 {
    _refireDelay -= Time.deltaTime;
    if (_refireDelay < 0)
    {
       Shoot();
       _refireDelay = RefireDelay;
    }
 }  

Keep your Shoot functions, and all public variables and transforms.

Don't use Time.time, use deltaTime, that's a time difference that elapsed between last frame and this one.

When comparing float values, never use == operator because it will never return true. Your float timers will never be EXACTLY 0 values, it will go from 0.005123 to -0.0110293 or something like that. So always check less or more, never equal, with time and other floating point values.

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.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to make a simple line of sight in a 2D top down shooter. 3 Answers

2D Top down shooter, gun is shaking while moving and Cursor solutions 0 Answers

Player sinking into walls and floor with Raycasts 1 Answer

Top Down Shooter Player Controller 1 Answer

2D raycast on mobile is giving weird results 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