Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Object Spawn on mouse button down

Discussion in 'Scripting' started by ddulshan, Jul 16, 2014.

  1. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    Hey guys, I'm trying to make a Simple shooting game. I want to spawn a object at the end of the gun, you know the place where the bullet comes out ;). I've made up a simple particle that can be used for this.

    Now my question is. How to make the particle that I've created to spawn at the end of the gun continuously until the mouse button is released. Yeah, I wonna make the light like thing appear when shooting. I just want the script for spawning this object. Please give me the script as simple as possible, 'cause I need to see how it works and I'm new to programming.

    Oh! And the script should be Java.
    Thanks in Advance.

    Sorry for my Bad English.
     
  2. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    Anyone? Please....
     
  3. Crayz

    Crayz

    Joined:
    Mar 17, 2014
    Posts:
    192
  4. randomperson42

    randomperson42

    Joined:
    Jun 29, 2013
    Posts:
    974
    Unity doesn't support Java...
     
  5. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
  6. TRALLALAL

    TRALLALAL

    Joined:
    Sep 7, 2013
    Posts:
    132
    Java and JavaScript are two different things.
     
  7. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    randomperson is referring to the scripting language in unity called "javascript" isn't anything like "java" (specifically the language called Java by oracle).

    it's not even the "javascript" used elsewhere outside unity... it's usually better to refer to it as "unityscript".
     
  8. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    Ok, my bad. But please some one give me the "Unityscript".
     
  9. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  10. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    Nope, it's different from what I'm looking for.

    I need to spawn a "Particle" at the end of the Gunbarrel. I've created a empty object name "Gun Barrel" at the place where I need the Particle to spawn. And I've finished the particle. I wants to know how to spawn this particle in the Empty Game Object named "Gun Barrel" when the mouse is pressed and until the mouse button is released.

    And this is what I've written so far.

    Code (JavaScript):
    1.  
    2. var barrelEnd : Transform;
    3. var gunFire : Particle;
    4.  
    5. function update () {
    6.      if(Input.GetButtonDown("Fire1")){
    7.           var fireInstance : Particle;
    8.           fireInstance = Instantiate(gunFire, barrelEnd.position, barrelEnd.rotation);
    9.           fireInstance.AddForce(barrelEnd.forward * 5000);
    10. }
    11.  
    I know there are many mistakes in it. Help me correct them and complete the sript.
     
  11. zDemonhunter99

    zDemonhunter99

    Joined:
    Apr 23, 2014
    Posts:
    478
    You can spawn particles just like normal bullet prefabs. Make a prefab of your flash particle, instantiate it when you shoot a bullet and immediately destroy the created effect.

    TL;DR You can use 'Instantiate' for spawning particles.
     
  12. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    @zDemon: Thanks! It works.

    Now I have to put a put a firesound to it.

    I used a var sound : audioclip wrote after the particle lines. when I write audio.play(sound); a error shows. but when used audio.playoneshot(sound); the sound plays but half finished.

    How to play the sound full when pressed LMB.
     
  13. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    Come on guys...
     
  14. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    Begging for code does not work. You need to figure it out for yourself. What you want is one of the easy parts of making a game. If you can't figure out how to do this, then this is not for you.
     
  15. ChrisSch

    ChrisSch

    Joined:
    Feb 15, 2013
    Posts:
    763
    Google tutorials for it! And youtube them! There's a loooot.
    As for the audio thing, if you only have one audio attached you don't need to type audio.Play(sound), just write audio.Play();
     
  16. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    Ok. Thanks. All fixed.