Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Grenade Script?

Discussion in 'Scripting' started by The3DKnight, Aug 19, 2011.

  1. The3DKnight

    The3DKnight

    Joined:
    Feb 8, 2011
    Posts:
    240
    Hi i'm trying to make a script for a grenade i wanted in for like a about 2 seconds to explod , and to get some range so that it can hurt player.
    Here :
    Code (csharp):
    1. var LifeTime = 1.0;
    2. var explosion = transform;
    3.  
    4. function Update ()
    5. {
    6. Destroy (gameObject, LifeTime);
    7.  
    8. }
     
  2. ar0nax

    ar0nax

    Joined:
    May 26, 2011
    Posts:
    485
    well is this a FPS?

    if so, then make a short script on your player to throw grenades in the direction of transform.forward, btw, place a rigidbody on the grenade, and when Input.GetKeyDown(KeyCode.G){//instantiate the grenade} then on Input.GetKeyUp(KeyCode.G){//grenade.rigidbody.AddForce(bla bla)}

    make sure getkeyup is true only after instantiation of the grenade in the player's hand...

    PS: i hope you understand what i mean... make the grenade a prefab, and place the script on it, and for the instantiation of explosion and such, you need to remake the destroy script.

    first, make a function Awake(){//set LifeTime here} then in function Update(){LifeTime -= Time.deltaTime; if(LifeTime< = 0){//instantiate explosion at current location, then destroy the gameobject}}

    I am sorry for not using CODE tags, but i am very tired...
     
  3. The3DKnight

    The3DKnight

    Joined:
    Feb 8, 2011
    Posts:
    240
    It's not FPS it's (3d person shooter).
     
  4. The3DKnight

    The3DKnight

    Joined:
    Feb 8, 2011
    Posts:
    240
    Nahh i just can't do it.. :(
     
  5. lmbarns

    lmbarns

    Joined:
    Jul 14, 2011
    Posts:
    1,628
    That same fps tutorial I mentioned in your other thread has a rocketlauncher script that shoots a projectile which explodes and does aoe damage to an area around the explosion point, which decreases by the distance you are from the point of contact.

    In the 1st pdf it sets up your "missile objects" in the second pdf you instantiate them and send them in a direction, destroy them and apply damage to anything hit. The 3rd pdf walks you through setting up enemy AI to shoot.

    Basically use your "grenade" in place of "missile object" and give it gravity on the rigidbody so it has an arc.
     
  6. The3DKnight

    The3DKnight

    Joined:
    Feb 8, 2011
    Posts:
    240
    Can someone write or give me the script because i really don't feel like dowloading FPS Tutorial just because of that script.If no one can i will download it as a last option.?
    Thank you
     
  7. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    It probably took you longer to write that post then to download the tutorial.....
     
    HolBol likes this.
  8. The3DKnight

    The3DKnight

    Joined:
    Feb 8, 2011
    Posts:
    240
    No i'm still downloading it.. :p
     
  9. Aweso

    Aweso

    Joined:
    Aug 19, 2011
    Posts:
    108
    Code (csharp):
    1.                        
    2.                         GameObject explo = Instantiate(ShootExplo,transform.position,transform.rotation) as GameObject;
    3.             Collider[] Arround = Physics.OverlapSphere(transform.position,15.0f);
    4.             Collider[] ArroundNear = Physics.OverlapSphere(transform.position,7.0f);
    5.                                    
    6.             foreach(Collider intoExp in ArroundNear)
    7.             {              
    8.                 if(intoExp.transform.tag == "Enemy")
    9.                 intoExp.collider.transform.gameObject.SendMessage("Dead");                         
    10.             }
    11.                                            
    12.             foreach(Collider inExp in Arround)
    13.             {                      
    14.                 if(inExp.transform.tag == "Enemy")
    15.                 inExp.collider.transform.gameObject.SendMessage("Explo");
    16.                
    17.                                            
    18.             }
    Thats Should do it,just add what you need and adjust it.
    and you can make count down timer -

    Code (csharp):
    1. float Timer = .0f;
    2. float TimeToExplo = 2.0f;
    3.  
    4. void Update()
    5. {
    6.     Timer += 1 * Time.deltaTime;
    7.      
    8. if(Timer >= TimeToExplo)
    9. {
    10. //your Code...
    11. // Destroy(Grenade);
    12.  
    13. }
    14. }
    15.  


    Also its in C# and I left you a lot of work,because I dont post codes..
    so figure it out here and show us we arnt wasting time on you =)
     
    Last edited: Aug 19, 2011
    Z0leee87 likes this.
  10. The3DKnight

    The3DKnight

    Joined:
    Feb 8, 2011
    Posts:
    240
    Uhh...Thanx i'll try the only problem now is it's funny i barely know java scripting let alone C# scripting...
    But thanx for your time. :)
     
  11. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    ahem... it is nearly the same:

    Code (csharp):
    1.  
    2. var Timer = 0.0f;
    3. var TimeToExplo = 2.0f;
    4.  
    5. function Update()
    6. {
    7.     Timer += 1 * Time.deltaTime;
    8.      
    9. if(Timer >= TimeToExplo)
    10. {
    11. //your Code...
    12. // Destroy(Grenade);
    13.  
    14. }
    15. }
    16.  
     
  12. Aweso

    Aweso

    Joined:
    Aug 19, 2011
    Posts:
    108
    It is just that you can add somecode after the 2Seconds when it should explode and then destroy it,and the code that I wrote - take alook at the first part of my answer.
     
  13. The3DKnight

    The3DKnight

    Joined:
    Feb 8, 2011
    Posts:
    240
    Ahh i give up i can't do it right by the way thanky you for your time.
     
  14. Ethaninja

    Ethaninja

    Joined:
    Jan 7, 2013
    Posts:
    277
    You will never learn if you don't try. Take the time to get to learn the fundamentals of scripting, such as functions and what Time.deltaTime is. At the very least how an if statement works. You should then be able to break it down from that.
     
    Z0leee87 likes this.
  15. asceansion

    asceansion

    Joined:
    Aug 13, 2015
    Posts:
    1
    Thank you, have successfully integrated this script into my fuel tanks to make them explode
     
  16. IronClad771

    IronClad771

    Joined:
    Jan 5, 2014
    Posts:
    7
    That code still works, just implemented it into a unity 5 multiplayer project.

    GRENADES FOR EVERYONE!!!
     
  17. Paloo

    Paloo

    Joined:
    Sep 23, 2020
    Posts:
    10
    Just use Brackey’s tutorial: