• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by fraubolat · Jun 27, 2020 at 10:07 AM · raycastparticlesprogrammingshootingscritping

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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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 ?

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fraubolat · Jun 27, 2020 at 06:22 PM 0
Share

oh ya that is right but do u now hot stop after i shoot ??? and thanks

avatar image Saint-Pasteque fraubolat · Jun 27, 2020 at 07:46 PM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

197 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Limited ammo from Raycast? 1 Answer

Look At Player with Clamping. 1 Answer

Arrow not shooting the in right orientation 0 Answers

Launch a projectile from one object to another 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges