• 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
Question by Omnimuu · Jun 01, 2013 at 05:57 AM · fpsgunparticlereloading

.Js Particle issue (probably easy for moderate coders)

Hi all, i am working on a gun, the barrel of which emits particles in place of bullets merely as a visual element, they do no damage.

all that is working ok but it continues to emit while the gun's reload sequence happens and the fire button is down, i need the emitter to stop producing whether the fire key is down or not during the reload, and then continue emitting only on the key press after.

to make thins worse the Emit_on_Keypress script is in one file and the Reload_Func. of the gun is part of another.

all i need is something to kill the emitter while the gun reloads and then let it go again when the reload is done (3.3 sec) while referencing commands between the two scripts.

i imagine this is easy if you know what you are doing but if you haven't guessed by now i am a total noob to coding, and have hit a wall.

-------------this is the first script that controls the gun--------------------------------
var Range : float = 1000;
var Force : float = 1000;
var Clips : int = 20;
var BulletPerClip : int = 60;
var ReloadTime : float = 3.3;
var Damage : int = 9;//this is the value of damage per hit
var BulletsLeft : int = 0;
var ShootTimer : float = 0;
var ShootCooler : float = 0.9;
public var ShootAudio : AudioClip;
public var ReloadAudio : AudioClip;
var HitParticles : ParticleEmitter;
var MuzzleFlash : ParticleEmitter;
var MuzzleCooler : float = 0.9;
var MuzzleFlashTimer : float = 0;
var Light1 : GameObject;
var Light2 : GameObject;
var Light3 : GameObject;
var MuzzleSpeed : int = 200000;
var KeyTimer : float = 0;
var KeyCooler : float = 1;

function Start (){ BulletsLeft = BulletPerClip; MuzzleFlash.emit = false; HitParticles.emit = false; Light1.active = false; Light2.active = false; Light3.active = false; }

function Update () { if( KeyTimer > 0){
KeyTimer -= Time.deltaTime;
}
if( KeyTimer < 0){
KeyTimer = 0;
}
if ( MuzzleFlashTimer > 0){
MuzzleFlashTimer -= Time.deltaTime;
MuzzleFlashShow();
}
if( MuzzleFlashTimer < 0){ MuzzleFlashTimer = 0;

}
if( ShootTimer > 0){
ShootTimer -= Time.deltaTime;
}
if( ShootTimer < 0){
ShootTimer =0;
}
if(KeyTimer == 0){ if(Input.GetMouseButton(0) && BulletsLeft){//GetKey(KeyCode.F) = rapid fire, GetKeyDown(KeyCode.F) = single fire, Replace with GetMouseButton(0)when ready 0=L, 1=R, 2=M if( ShootTimer == 0){
PlayShootAudio(); RayShoot();
ShootTimer = ShootCooler; }
if ( MuzzleFlashTimer ==0){
MuzzleFlashTimer = MuzzleCooler; MuzzleFlashShow();

} } } }

function MuzzleFlashShow (){

if(MuzzleFlashTimer >0){
MuzzleFlash.emit = false;
Light1.active = false; Light2.active = false; Light3.active = false;
} if ( MuzzleFlashTimer == MuzzleCooler ){

  MuzzleFlash.transform.localRotation = Quaternion.AngleAxis(Random.value * 360 * MuzzleSpeed , Vector3.forward);    
  MuzzleFlash.emit = true;
    
         Light1.active = true; 
         Light2.active = true;
         Light3.active = true;         

}
}

function RayShoot (){ var Hit : RaycastHit;
var DirectionRay = transform.TransformDirection(Vector3.forward);
Debug.DrawRay(transform.position , DirectionRay Range , Color.blue); if(Physics.Raycast(transform.position , DirectionRay , Hit, Range)){
if(Hit.rigidbody){
if( HitParticles){
HitParticles.transform.position = Hit.point; HitParticles.transform.localRotation = Quaternion.FromToRotation(Vector3.forward, Hit.normal); HitParticles. Emit();
Hit.rigidbody.AddForceAtPosition( DirectionRay
Force , Hit.point);
Hit.collider.SendMessageUpwards("ApplyDamage" , Damage, SendMessageOptions.DontRequireReceiver);
} } }
BulletsLeft --;
if(BulletsLeft < 0){
BulletsLeft = 0;
}
if(BulletsLeft == 0){
Reload ();
}
} function Reload (){ PlayReloadAudio(); yield WaitForSeconds( ReloadTime);
if(Clips > 0){
BulletsLeft = BulletPerClip; } }

function PlayShootAudio (){ audio.PlayOneShot( ShootAudio); }

function PlayReloadAudio (){ audio.PlayOneShot( ReloadAudio);

}

------------------------------And the Emit on KeyPress-----------------------------

var emissionTime : float = 3.0;

var emissionDelay : float = 3.0;

var lastTime = 0.0;

function Update() { if (Input.GetMouseButton(0)){ GetComponent(ParticleAnimator).autodestruct = false; particleEmitter.emit = true; lastTime = Time.time; } if (particleEmitter.emit && (Time.time - lastTime) > emissionTime) { particleEmitter.emit = false; }

}


there is probably an easier way to get the desired result, i would take that too, this is all just stuff i stitched together watching tuts online.

Any help would be appreciated, thanks!.

Comment

People who like this

0 Show 0
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

0 Replies

· Add your reply
  • Sort: 

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

14 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

Related Questions

Particles not appearing on impact, muzzle flash problems? 2 Answers

Fps game gun reloading problem 1 Answer

How do I make a gun project a particle? 1 Answer

Reload help 1 Answer

How do I destroy a gameobject after 5 seconds 2 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