- Home /
problem with scripting
i have this script and the problem is when ever i press fire it works fine but then the fire particle system keep spawning even though i am not pressing fire. here is the script
using System.Collections;
using System.Collections.Generic;
using System.Text;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class shootak47 : MonoBehaviour
{
public Transform wheretoshoot;
int range = 1000;
private bool ishooting = false;
bool isreloading = false;
public int bullet = 30;
public int currentbullet;
const float fireDelay = 0.13f;
float delayTimer = 0;
public GameObject sprite;
enemyhealth health;
realhealth Realhealth;
float random = 0.03f;
public GameObject bulleteffect;
public Transform wheretospawnbuleltefect;
public ParticleSystem smoke;
public ParticleSystem fire;
void Start()
{
currentbullet = bullet;
smoke.Pause();
fire.Stop();
}
void Update()
{
if (delayTimer > 0)
{
delayTimer -= Time.deltaTime;
if (delayTimer < 0) delayTimer = 0;
}
if (Input.GetButton("Fire1"))
{
if (delayTimer == 0)
{
shooting();
delayTimer = fireDelay;
}
}
}
public void shooting ()
{
if (currentbullet == 0)
{
smoke.Play();
return;
}
if(!ishooting)
{
RaycastHit Hit;
Vector3 dic = wheretoshoot.transform.forward;
dic = dic + wheretoshoot.TransformDirection(new Vector3(Random.Range(-random, random), Random.Range(-random, random)));
if (Physics.Raycast(wheretoshoot.position, wheretoshoot.TransformDirection(Vector3.forward), out Hit, range))
{
fire.Play();
Instantiate(bulleteffect, wheretospawnbuleltefect.position, Quaternion.Euler(new Vector3(Random.Range(-90,90),Random.Range(-90,90))));
Instantiate(sprite, Hit.point, Quaternion.LookRotation(Hit.normal));
Realhealth = Hit.transform.GetComponent<realhealth>();
health = Hit.transform.GetComponent<enemyhealth>();
if(health != null )
{
health.takedamge(20);
}
if(Realhealth != null)
{
Realhealth.takedamge(20);
}
}
}
currentbullet--;
if(currentbullet == 0)
{
StartCoroutine(delaybetweenshots());
}
}
IEnumerator delaybetweenshots()
{
yield return new WaitForSeconds(2f);
currentbullet = bullet;
smoke.Stop();
}
}
Comment
Answer by Saint-Pasteque · Jun 27, 2020 at 10:47 AM
@fraubolat Hey, i see in line 80-80 :
**fire.Play();**
but you never do fire.Stop() except in the start()
Maybe it's what you've done wrong ?
oh ya that is right but do u now hot stop after i shoot ??? and thanks
I will suppose by fire you mean "muzzle flash". If i'm right, juste use a coroutine that stop after 0.5 second as you did for smoke.