- Home /
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
Your answer
Follow this Question
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