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 Mjhurtado1 · Jan 18, 2015 at 10:12 PM · scripting problemshootinggungetbuttondown

GetButtonDown problem | C#

Hello, I'm trying to make a weapon script that allows me to change the firing mode from being able to hold down the trigger on a machine gun, or make it so you have to keep clicking for the gun to keep firing, like on a pistol.

There is one problem tho, when the script uses GetButtonDown, it no longer works.

 using UnityEngine;
 using System.Collections;
 
 public class thePlayerShoot : MonoBehaviour {
 
 
     private bool cooling = false;
     public float cooldown = 0.7f;
     private float maxcooldown;
     public AudioClip gunshot;
     public AudioClip gunhit;
     public string checkTag = "enemy";
     public GameObject blood;
     public GameObject effect1;
     public GameObject effect2;
     public int gunPower = 5;
     public int dist = 70;
     public GameObject theCamera;
     public GameObject gunBarrel;
 
 
     private int oldGunPower;
 
     public GameObject gun;
     private bool special = false;
     private float specialTime = 15;
     public Material defaultMat;
     public Material specialMat1;
     public GameObject specialEffect;
     public AudioClip instaKill;
 
     public int health = 3;
     public GameObject explosion;
     public AudioClip explosionaudio;
 
     public bool canHoldTrigger = true;
 
 
     
     void Start () {
         maxcooldown = cooldown;
         oldGunPower = gunPower;
     }
 
     void Update () {
 
                 if (Time.timeScale == 0)
                         return;
 
 
                 if (health <= 0)
                         Application.LoadLevel ("holdTheLine");
 
                 if (special == true) {
                         specialTime -= Time.deltaTime;
                         gun.gameObject.renderer.material = specialMat1;
                         gunPower = 100;
                 }
 
                 if (specialTime <= 0) {
                         gun.gameObject.renderer.material = defaultMat;
                         special = false;
                         specialTime = 15;
                         gunPower = oldGunPower;
                 }
 
 
                 if (cooling == true) {
                         cooldown -= Time.deltaTime;
                 }
 
                 if (cooldown < 0) {
                         cooling = false;
                         cooldown = maxcooldown;
                 }
 
 
 
 
 
 
 
         if (canHoldTrigger == true)
                 if (Input.GetButton ("Fire1"))
                     Shoot();
             
 
         if(canHoldTrigger == false)
             if (Input.GetButtonDown ("Fire1"))
                 Shoot();
 
 }
 
 
 
 
 
 
 
     public void Shoot(){
 
         RaycastHit hit = new RaycastHit ();
         bool foundHit = false;
 
 
         foundHit = Physics.Raycast (transform.position, transform.forward, out hit, dist);
         if (cooling == false) {
             audio.PlayOneShot (gunshot, 0.4F);
             Instantiate (effect1, gunBarrel.gameObject.transform.position, effect1.transform.rotation);
             Instantiate (effect2, gunBarrel.gameObject.transform.position, theCamera.transform.rotation);
             cooling = true;    
             if (foundHit) {
                 if (hit.transform.tag == checkTag) {
                     print ("Hit an enemy!");
                     audio.PlayOneShot (gunhit, 0.6F);
                     Instantiate (blood, hit.transform.position, blood.transform.rotation);
                     theEnemy enemyScript = hit.transform.GetComponent<theEnemy> ();
                     enemyScript.health -= gunPower;
                 } else { 
                     if (hit.transform.tag == "dontshoot") {
                         audio.PlayOneShot (gunhit, 0.6F);
                         audio.PlayOneShot (explosionaudio, 0.7F);
                         health -= 1;
                         hit.transform.gameObject.AddComponent<Rigidbody> ();
                         hit.rigidbody.AddForce (0, 1000, 0);
                         hit.rigidbody.AddForce (0, 1000, 0);
                         Instantiate (explosion, hit.transform.position, explosion.transform.rotation);
                         Instantiate (explosion, hit.transform.position, explosion.transform.rotation);
                         Destroy (hit.transform.gameObject, 3);
                     } else {
                         if (hit.transform.tag == "special") {
                             special = true;
                             audio.PlayOneShot (gunhit, 0.6F);
                             audio.PlayOneShot (instaKill, 2);
                             Instantiate (specialEffect, hit.transform.position, specialEffect.transform.rotation);
                             Destroy (hit.transform.gameObject);
                         }
                         
                         
                         if (hit.transform.tag == "pushable") {
                             hit.rigidbody.AddForceAtPosition (10 * gun.transform.forward, hit.point);
                             audio.PlayOneShot (gunhit, 0.6F);
                         }
                     }
                 }
             }
         }
     }
 
 
 
 }



There are no errors in the console.

Any help would be greatly appreciated, thanks!

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

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

How can I shoot bullets that you can aim with the crosshair? 1 Answer

1 script doesn't react to input 1 Answer

FPS recoil 1 Answer

How to add sound to gun shot script 4 Answers

Variable Projectile speed that changes based on distance from Target 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