#Raycasts
1 messages · Page 1 of 1 (latest)
Sounds like you might be overriding your particles Play function - what does your code look like?
this is my "gun" code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Gun : MonoBehaviour
{
//random
public ParticleSystem muzzleFlash;
public GameObject impactEffect;
//floats
public float damage = 10f;
public float range = 100f;
public float impactForce = 30f;
public float fireRate = 15f;
private float nextTimeToFire = 0f;
//Camera
public Camera fpsCam;
//Voids
void Update()
{
if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire)
{
nextTimeToFire = Time.time + 1f / fireRate;
Shoot();
}
}
void Shoot()
{
muzzleFlash.Play();
RaycastHit hit;
if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
{
Debug.Log(hit.transform.name);
Target target = hit.transform.GetComponent<Target>();
if (target != null)
{
target.TakeDamage(damage);
}
if (hit.rigidbody != null)
{
hit.rigidbody.AddForce(-hit.normal * impactForce);
}
GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(impactGO, 2f);
}
}
}
Your code seems fine to me, you could try decreasing your fireRate so your nextTimeToFire takes longer - if your muzzle flash for example lasts 2 seconds, and you have a very low fire rate that will make nextTimeToFire longer than 2 seconds, continuing to hold down the button should see your muzzle flash play fully - alternatively you could try checking if the particle system is already playing, and only call Play if it is not already playing