- Home /
i want the particle effect to play each time i press the mouse button but to imitate muzzle flash but it only plays the first time i press the mouse button and does not play the rest of time
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class gunfirealpha : MonoBehaviour { public float damage = 10f; public float range = 100f; public float hr = 15f; public Camera fps; public ParticleSystem p; public GameObject impacteff; //AudioClip aud=transform.GetComponent(); // Start is called before the first frame update private float next = 0f; AudioSource bulletaud; public float bulletSpeed = 1100; public GameObject bullet; void Start() { bulletaud = GetComponent();
}
// Update is called once per frame
void Update()
{
if (Input.GetButton("Fire1") && Time.time >= next)
{
next = Time.time + (1 / hr);
shoot();
Fire();
}
}
void shoot()
{
p.Play();
RaycastHit hit;
if (Physics.Raycast(fps.transform.position, fps.transform.forward, out hit, range))
{
Debug.Log(hit.transform.name);
target t = hit.transform.GetComponent<target>();
if (t != null)
{
t.TakeDamage(damage);
}
if (hit.rigidbody != null)
{
hit.rigidbody.AddExplosionForce(5f, hit.point, 1f);
}
}
GameObject eff = Instantiate(impacteff, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(eff, 2f);
}
void Fire()
{
//Shoot
GameObject tempBullet = Instantiate(bullet, transform.position, transform.rotation) as GameObject;
Rigidbody tempRigidBodyBullet = tempBullet.GetComponent<Rigidbody>();
tempRigidBodyBullet.AddForce(tempRigidBodyBullet.transform.forward * bulletSpeed);
Destroy(tempBullet, 0.5f);
//Play Audio
bulletaud.Play();
}
}
Your answer
Follow this Question
Related Questions
how to edit waterfall 1 Answer
particle effect activation 1 Answer
How can I create a particle 'vortex' or implosion using Shuriken? 2 Answers
How can I click the particles emitted from a Particle System 1 Answer
Making ice/glass shatter without thousands of tiny cubes!(probably particle effect) 2 Answers