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 Crumpet · Mar 14, 2014 at 01:32 PM · raycastingbulletgunfirereload

c# weapon shooting script using raycasting help

So I'm creating this script to make my gun fire, so far I have it reloading exactly how I want it to I would just like to make the weapon shoot. I looked up a few tutorials and found that most are using java script so I didn't really learn much and I ended up here http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html which I read but I wasn't sure how to implement this into my script to make the gun fire I was hoping to make it similar to this video https://www.youtube.com/watch?v=mpxim8YbsMk&feature=youtu.be except using c#. and yes since I got my post blocked earlier, there are many ways of making a bullet shoot. I'm just looking for one of the ways.

 using UnityEngine;
     using System.Collections;
      
     public class NewBehaviourScript : MonoBehaviour
     {
     public bool outofammo;
     public int bullets = 7;
     public GameObject deagle; //my gun
     public float time = 1;
     public float firerate = 1;
     public bool fired = false;
     public bool Cannotfire = false;
     public bool timedown;
      
     void Update ()
     {
     if (Input.GetKeyDown(KeyCode.R) && bullets < 7)
     {
     deagle.animation.Play ("Reload");
     bullets = 7;
     }
      
     if (Input.GetButtonDown("Fire1"))
     {
     timedown = true;
      
     if (bullets == 0)
     {
     bullets = 7;
     outofammo = true;
     timedown = false;
     }
     else
     outofammo = false;
      
     if (outofammo == true)
     {
     deagle.animation.Play ("Reload");
     }
      
     }
      
     if (timedown == true)
     {
     time = (time - (firerate *Time.deltaTime));
     if (time <= 0)
     {
     bullets = bullets -1;
     fired = true;
     timedown = false;
     time = 1f;
     fired = false;
     }
     }
      
     if (fired == true)
     time = 1;
     }
     }
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 TykoX64 · Mar 14, 2014 at 02:46 PM 1
Share

On the script reference page, right at the top should be an option to change the code from javascript to C#. It's located in the upper left.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by TykoX64 · Mar 14, 2014 at 02:51 PM

Basically you would create a ray starting from your gun and pointing the direction you're firing and test it with Physics.raycast. you also need a HitInfo variable so that you can test the collision. The last option on the script reference shows this very well. http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html

instead of using ScreenPointToRay() you can create a new Ray(gun.position, gun.fireDirection).

Comment
Add comment · Show 12 · 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 Crumpet · Mar 14, 2014 at 04:59 PM 0
Share

Okay so I edited my script and now I have an error similar to this one

http://answers.unity3d.com/questions/27678/nullreferenceexception-object-reference-not-set-to.html

my error being: NullReferenceException: Object reference not set to an instance of an object NewBehaviourScript.Update () (at Assets/Scripts/NewBehaviourScript.cs:34)

I can see how to fix it from that link but I'm not sure how I would apply the same technique to my script

 using UnityEngine;
 using System.Collections;
 
 public class NewBehaviourScript : $$anonymous$$onoBehaviour
 {
     public bool outofammo;
     public int bullets = 7;
     public GameObject deagle; //this is my gun
     public float time = 1;
     public float firerate = 1;
     public bool fired = false;
     public bool Cannotfire = false;
     public bool timedown;
     Transform Effect;
     float Damage = 100;
 //    object particleClone;
     RaycastHit hit;
 
     void Update ()
     {
             if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.R) && bullets < 7) 
                 deagle.animation.Play ("Reload"); 
             }
 
             if (Input.GetButtonDown("Fire1")) 
             {
                 timedown = true;
                 Vector3 forward = transform.TransformDirection (Vector3.forward);
         
             
 
                 hit.transform.Send$$anonymous$$essage ("ApplyDamage", Damage, Send$$anonymous$$essageOptions.DontRequireReceiver);
 
                 if (bullets == 0) 
                 {
                     bullets = 7;  to being 7
                     outofammo = true; 
                     timedown = false; 
                 }
                 else 
                     outofammo = false; 
 
                 if (outofammo == true)
                 {
                     deagle.animation.Play ("Reload"); 
                 }
 
             }
             
             if (timedown == true) 
             {
                 time = (time - (firerate *Time.deltaTime)); 
                 if (time <= 0) 
                 {
                     bullets = bullets -1; 
                     fired = true; 
                     timedown = false; 
                     time = 1f;
                     fired = false;
                 }
             }
             
             if (fired == true) 
                 time = 1;
     }
 }
avatar image Berenger · Mar 14, 2014 at 05:01 PM 0
Share

I converted your answer to a comment, did you mean to comment on Tyko answer ?

avatar image Crumpet · Mar 14, 2014 at 05:04 PM 0
Share

Yeah, thanks but anyone is welcome to answer. :)

avatar image Dakwamine · Mar 14, 2014 at 05:07 PM 0
Share

I think there are missing lines in your last code copy/paste. At line 34, there is only the bullets variable, so this one cannot be null...

avatar image Crumpet · Mar 14, 2014 at 05:13 PM 0
Share

Huh what do you mean? I wrote everything myself apart from the raycasting part, lines 14-17, 28 and 32. Do you mean my bullets can't be equal to 0? because I created a fire-rate by using

    time = (time - (firerate *Time.deltaTime));
     if (time <= 0) 

this is where my bullets are taken away so if my bullets = 0 it will automatically reload and fill the clip back to 7. Is that what you mean?

Show more comments

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

23 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

Related Questions

How can I shooting script c#? 0 Answers

Make a Gun fire multiple times 3 Answers

Ammo Script 1 Answer

Best way to make a gun? 1 Answer

How do shoot a bullet when the right button is clicked? 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