Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 Ralp · Apr 04, 2013 at 05:24 PM · c#particletoggleemitter

Need help with weapon firing particles

What I need to do is simple: when firing my gun, it emits a prefab particle once, repeating it every fire. Although it looks quite simple, it sounds nearly impossible to not do this without triggering an error. Below is what I already tried to do:

  • The particle autodestructs when its over, then is recreated on the next fire. I tried using Instantiate, Resources.Load, GetComponent and alike but I cant get a clue on what is wrong.

  • Create a second script for the particle, which receives data from the gun script saying when to emit and when to stop. Problem is I cant figure out how to get data from a different object's script.

  • Get the exact time I should order my particle to stop emitting (also by scripting it). By far the most viable solution in my opinion, but certainly impractical.

A C# script would be appreciated.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Ralp · Apr 05, 2013 at 12:39 AM

After having a look at the codes I made back on my XNA lessons and the other answer here, I finally solved this puzzle.

On the gun script, add a public static variable which will be used on other scripts. It will receive the countdown from the private variable, which is the one being worked:

 public static float cooldown{get;set;}
 float cooldownCounter;
 bool isFiring
 
 void Update()
 {
   if(isFiring)
   {
     cooldownCounter++;
     if(cooldownCounter < maxTime) isFiring=false;
   }
   else if(Input.GetButton("Fire")
   {
     <code related to firing gun>
     isFiring=true;
   }
   cooldown = cooldownCounter;
 }
 

Make sure the particle prefab has Emit turned off on the emitter. On the particle script, use the following code on Update to emit the particle when the counter starts and then turn it off after a certain time:

 if(ShotgunFire.cooldown>0) particleEmitter.emit=true;
 if(ShotgunFire.cooldown>10) particleEmitter.emit=false;

Not sure if this is how a "get set" protection is done, but at least it works.

Comment
Add comment · 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
1
Wiki

Answer by Dracorat · Apr 04, 2013 at 06:43 PM

Here's what I'm using - the only part missing is "Get the exact time I should order my particle to stop emitting" so I'll just add some additional information after the script.

This is only one of many possible permutations of this functionality:

 //These are my class variables relating to firing a weapon
 private float lastFire = -50;
 public float fireCooldown = 0.1f;
 public Rigidbody Laser;
 public float BaseLaserSpeed = 200f;
     private GameObject gun1;
     private GameObject gun2;

 //In the Start routine (which is called when the player object is created.)
 gun1 = GameObject.Find("Gun1");
 gun2 = GameObject.Find("Gun2");


 //On each Update, I call a method with the following code:
     this.lastFire -= Time.deltaTime;        
     if(Input.GetMouseButton(0)){
         if(this.lastFire <= 0){
             this.lastFire = this.fireCooldown;
             Rigidbody laser = (Rigidbody) Instantiate(this.Laser, gun1.transform.position, gun1.transform.rotation);
             laser.velocity = laser.transform.forward * this.BaseLaserSpeed;
             
             laser = (Rigidbody) Instantiate(this.Laser, gun2.transform.position, gun1.transform.rotation);
             laser.velocity = laser.transform.forward * this.BaseLaserSpeed;
         }
     }

As for sending data to the object (like "when to emit" and "when to stop" - you can do that by making some public properties in the class definition for the other object.

After the Instantiate of your prefab:

 Rigidbody laser = (Rigidbody) Instantiate(this.Laser, gun1.transform.position, gun1.transform.rotation);
   laser.velocity = laser.transform.forward * this.BaseLaserSpeed;

You can get the script object attached. (Let's say it's called "LaserBehavior" in the editor) - and let's say you need to set StartTime

 laser.GetComponent(LaserBehavior).StartTime = Time.realtimeSinceStartup;

In order to support this, the other script needs something like this:

 public realtime StartTime { get; set; }

Comment
Add comment · Show 1 · 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 Ralp · Apr 04, 2013 at 10:44 PM 0
Share

Thanks a lot! I figured it out a different way, but your answer is helpful.

EDIT: too bad im not allowed to vote :(

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.

Follow this Question

Answers Answers and Comments

11 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

Related Questions

How do I generate - IN CODE - a ParticleSystem for static particles? 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Water emission using c# 1 Answer

All instances of particle emitter prefab are emitting when emit=true 0 Answers


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