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);
}
}
}
↧