- Home /
Javascript Gun Firing Script Edit
Hello today I have made a gun script, it is working fine except that you can fire the gun everywhere even if you are not holding it. As you can imagine this is a problem. (DO NOT GIVE ME YOUR SCRIPT, JUST FIX THIS ONE)
Pick Up Script, may be useful for work but also might not
var SpawnTo: Transform;
var Gun : Transform;
function OnTriggerEnter(hit:Collider)
{
if(hit.gameObject.tag == "Player")
{
Gun.parent = SpawnTo;
Gun.transform.position = SpawnTo.transform.position;
Gun.transform.rotation = SpawnTo.transform.rotation;
}
}
Gun Firing Script #pragma strict var Effect : Transform; var TheDammage = 30; var AudioShot : AudioClip;
function OnTriggerEnter () {
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));
if (Input.GetMouseButtonDown(0))
audio.PlayOneShot(AudioShot,0.7);
if (Input.GetMouseButtonDown(0))
{
if (Physics.Raycast (ray, hit, 100))
{
var particleClone = Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(particleClone.gameObject, 2);
hit.transform.SendMessage("ApplyDammage", TheDammage, SendMessageOptions.DontRequireReceiver);
}
}
}
Answer by robertbu · Apr 30, 2014 at 07:04 PM
An easy solution. Change line 8 to:
if ((transform.parent != null) && Input.GetMouseButtonDown(0))
This assumes that when your gun is on the ground it has no parent, and that you return the parent to null if the gun is dropped.
Second script line 8...the one that currently has a Get$$anonymous$$ouseButtonDown() call.
Your answer
Follow this Question
Related Questions
aim down sights of gun 3 Answers
Avert Fate Style FPS Gun Script 3 Answers
Shoot Gun Aim Bullet? 1 Answer
How to move gun from middle of screen to right of screen? 1 Answer