- Home /
Sparks for unity raycast gun script
I have a raycast gun script and for the particle spark effect i have use Var Effect : Transform; and when i got to drag my sparks into the effect box the sparks wont work when i shoot my gun.
var endBarrel :Transform;
var sound : AudioClip;
var TheDamage = 100;
var Effect : Transform;
var Enemy : GameObject[];
function Start()
{
}
function Update()
{
var hit : RaycastHit;
if(Input.GetButtonDown("Fire1"))
{
// you can use PlayOneShot to specify the sound...
audio.PlayOneShot(sound);
audio.Play();
}
if (Input.GetMouseButtonDown(0))
{
if (Physics.Raycast (endBarrel.position, transform.forward, hit, 500))
{
Debug.DrawRay(endBarrel.position, endBarrel.forward * 100);
hit.transform.SendMessage("ApplyDamage", TheDamage, SendMessageOptions.DontRequireReceiver);
}
}
}
Uhh... You're not using Effect anywhere. You declare it, but you never tell the code what to do with it.
Answer by KillTheCrawler · Jun 27, 2013 at 07:24 PM
Try this. Make your effect variable a GameObject and create a new variable and call it something like Muzzle and make it a GameObject. The muzzle should be an empty by the way. Place the muzzle in front of your gun then type this into the update function:
function Update ()
if(Input.GetButtonDown("Fire1"))
{
Instantiate(Effect, Muzzle.transform.position, Muzzle.transform.rotation);
}
Best of luck!
That works but i dont want it for a muzzel flash i want it for when the ray hits something, also how do i make the particles destory after like 5 seconds after there created? Thanks for your help! :)
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Adding Rigidbody to an Object on Collision by Raycast? 1 Answer
Raycasting2D please help 1 Answer
Particle System help 0 Answers
Raycast Destroys player. 1 Answer