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 /
avatar image
0
Question by faggottripti · Jun 26, 2016 at 06:01 AM · c#scripting problemscripting beginnershooting

I can't make my player to shoot faster (Check script for reference)

Hello everyone! I was making a player shooting script and was having some trouble. The normal shooting works just fine, but there's a problem with the part where I increase the rate of fire. There is a collider (which is a trigger) in the script, which activates the shooting frenzy for 10 seconds. But when I touch the collider nothing happens. Does anyone know what's wrong here? Thanks. using UnityEngine;

     public class PlayerShooting : MonoBehaviour
     {
         
         public int damagePerShot = 20;
         public float timeBetweenBullets = 0.15f;
         public float range = 100f;
         public float delay = 1f;
         float nextUsage;
         float timer;
         int time = 0;
         Ray shootRay;
         RaycastHit shootHit;
         int shootableMask;
         ParticleSystem gunParticles;
         LineRenderer gunLine;
         AudioSource gunAudio;
         Light gunLight;
         float effectsDisplayTime = 0.2f;
     
     
         void Awake ()
         {
             shootableMask = LayerMask.GetMask ("Shootable");
             gunParticles = GetComponent<ParticleSystem> ();
             gunLine = GetComponent <LineRenderer> ();
             gunAudio = GetComponent<AudioSource> ();
             gunLight = GetComponent<Light> ();
             nextUsage = Time.time + delay;
         }
     
     
         void Update ()
         {
             timer += Time.deltaTime;
     
             if(Input.GetButton ("Fire1") && timer >= timeBetweenBullets && Time.timeScale != 0)
             {
                 Shoot ();
             }
     
             if(timer >= timeBetweenBullets * effectsDisplayTime)
             {
                 DisableEffects ();
             }
         }
     
         void OnTriggerEnter(Collider c1)  //to increase rate of fire
         {
             if(time <= 10)
             {
                 timeBetweenBullets = 0.05f;
                 if(Time.time > nextUsage)                    //to make it execute once per second.
                 {
                     nextUsage = Time.time + delay;
                     time++;                                    //to count seconds
                 }
             }
             else
             {
                          timeBetweenBullets = 0.15f;
                              time = 0;
                  }
 
         }
     
         public void DisableEffects ()
         {
             gunLine.enabled = false;
             gunLight.enabled = false;
         }
     
     
         void Shoot ()
         {
             timer = 0f;
     
             gunAudio.Play ();
     
             gunLight.enabled = true;
     
             gunParticles.Stop ();
             gunParticles.Play ();
     
             gunLine.enabled = true;
             gunLine.SetPosition (0, transform.position);
     
             shootRay.origin = transform.position;
             shootRay.direction = transform.forward;
     
             if(Physics.Raycast (shootRay, out shootHit, range, shootableMask))
             {
                 EnemyHealth enemyHealth = shootHit.collider.GetComponent <EnemyHealth> ();
                 if(enemyHealth != null)
                 {
                     enemyHealth.TakeDamage (damagePerShot, shootHit.point);
                 }
                 gunLine.SetPosition (1, shootHit.point);
             }
             else
             {
                 gunLine.SetPosition (1, shootRay.origin + shootRay.direction * range);
             }
         }
     }
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 StormMuller · Jun 26, 2016 at 09:16 PM 0
Share

Is Delay or timeBetweenBullets the variable that you want to control the speed?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by pronobroy · Jun 26, 2016 at 10:05 AM

I think your shootableMask ignore that ray to collider.Try with out shootableMask for your Physics.Raycast

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Argument out of range. Parameter name: index 1 Answer

How can i place subjects in different positions? 0 Answers

How to move the object to where the object is already pointing to? 1 Answer

Why when using get; set; in one script i'm getting null in other script that use it ? 1 Answer

Can someone explain calling other scripts in C#? 2 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