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 PaulDabest · Jun 28, 2016 at 04:49 AM · fpsshootinggun scriptfirst person shootergunfire

How to make gun shoot in the crosshair & automatic fire (Javascript)

Hello guys, So im new in unity and coding and i want a bit of help. I have a script that i found in a tutorial in youtube where i can shoot a single bullet and the bullet will go where the weapon is pointing. The problem is that i want it to go perfectly in the center of my screen, where i have made my crosshair. Also how can i create the automatic fire mode? I mean that if I simply click, i will shoot 1 bullet whereas if i hold down the left mouse button it will continue shooting until i hit the ammo limmit. I would love to be able if you could add a variable where it will have a delay between each bullet, that i can change between the weapons.

I forgot to mention earlier that i don't have a bullet "Object". Also the script is on the gun itself and i have a separate script where i basically say, where i press "Fire1" it would play an animation and a sound. I don't know if this will help but i thought it may have to do with something...

You can take a look in the script below. Thanks anyways :)

 var DamageAmount : int = 32;
 var TargetDistance : float;
 var AllowedRange : float = 100;
 
 function Update () {
     if(Input.GetButtonDown("Fire1")) {
 
         var Shot : RaycastHit;
             if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), Shot)) {
                 TargetDistance = Shot.distance;
                     if (TargetDistance < AllowedRange) {
                         Shot.transform.SendMessage("DeductPoints", DamageAmount);
                     }
                 }
             }
         }
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 PaulDabest · Jun 26, 2016 at 06:58 PM 0
Share

Please do not flame. I have absolutely no idea of coding. I am more in the 3d workspace than in the coding stuff which i really want to get into :)

avatar image Owen-Reynolds PaulDabest · Jun 28, 2016 at 04:48 AM 0
Share

If you're worried, the HelpRoom area is fine for Qs like this (I moved it there.) There's also no wait for a moderator to approve it.

But you should be able to look those up separately - shooting delay and various bullet raycasts. Lots of old Qs about that here.

avatar image PaulDabest Owen-Reynolds · Jun 28, 2016 at 10:36 AM 0
Share

Ok thanks :)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by UrielKane · Jun 30, 2016 at 04:36 AM

Well the fire rate task is almost really simple and it's a metter of one line of code and one sentence if(). This can be done in many ways like almost everything in coding. But one way wich is what i usually use is the Time class. I.E.

 public var FireRate : float = 0.1f;
 private var FireTimer : float;
 
 function Update () {
     if(Time.time > FireTimer && Input.GetButton("Fire1")){
        StartCoroutine(FireOneShot());
        FireTimer = Time.time + FireRate; 
        //this line of code can be put on the FireOneShot coroutine to. 
     }
 }
 
 function FireOneShot () {
     //Do the shot logic.
 }


The only problem with this solution is that sometimes you need to run it on FixedUpdate in order to have frame independency in the firerate. If you want frame independecy don't forget to use Time.deltaTime insted of Time.time.

On the other hand the logic for making the shot to be cast exactly through the crosshair. Depend on what exactly the circunstances and your needs about that. Becouse although the simple solution is to cast the ray using something like.

 function Update() {
     var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
 
     if (Physics.Raycast (ray, 100)) {
         print ("Hit something");
     }
 }

Sometimes you will need something more complex for example. Casting a ray from the gun position when is shot a lasser or something like that. In those cases you will need to cast two rays or use linecast instead, in order to check where the ray should be directed. I am currently just developing this system I just mentioned. I'm using a ray to check if in the point of the shoot is hitting something. The i change the direction of the second ray based on the hit info and so on. Then finally i have change completely the projectile behaviour for me rockets and else. Using linecast in one hand and on the other using triggers instead of clasic colisions.

I hope this will be helpfull. @PaulDabest

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make an automatic fire mode for a gun (Javascript) 0 Answers

My gun is only shooting bullets every 8th shot..? 0 Answers

Creating Random Target Points on Game Object for Player to Shoot 0 Answers

Help With Recoil Script 0 Answers

how can i set the direction of my gun to the Raycast collision point? 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