Search Unity

Grenade Change

Discussion in 'Scripting' started by The3DKnight, Feb 13, 2013.

  1. The3DKnight

    The3DKnight

    Joined:
    Feb 8, 2011
    Posts:
    240
    Hi. So i was trying to change grenades that i throw when i press "h".But how can i do it , i mean how can i change Bullet Prefab.
    I have my grenade throwing script here.

    Code (csharp):
    1.  
    2. public var BulletPrefab : Transform;
    3.  
    4. public var BulletSpeed : float = 2500;
    5.  
    6.  
    7. function Update (){
    8. if(Input.GetKeyDown ("h"))
    9. {
    10.  
    11. //BulletPrefab change to Grenade1
    12. //If input h again change to Grenade2
    13. //or again change to Grenade3
    14.  
    15. }
    16.  
    17.    }
    18.    
    19. function Throw ()
    20.  
    21. {
    22. Gammo -= 1;
    23. yield WaitForSeconds (0.1);
    24. var bulletCreate = Instantiate (BulletPrefab, GameObject.Find("SpawnPoint").transform.position, GameObject.Find("SpawnPoint").transform.rotation );
    25.           bulletCreate.rigidbody.AddForce(transform.forward * BulletSpeed);
    26.  
    27.  
    28. }
    29.  
    30.  
    31.  
    32.  
    Should i do something like
    Code (csharp):
    1.  
    2. var Grenade 1 : Transform;
    3. var Grenade 2 : Transform;
    4. var Grenade 3 : Transform;
    5.  
    6. //..........
    7.  
    8. if(Input.GetKeyDown ("h"))
    9. {
    10. BulletPrefab = Grenade 1;
    11. //and what next
    12. }
    13.  
    14.  
    15.  
    Someone knows how to do this?Or what should i do?
    Thank you
    -3DK
     
  2. nullstar

    nullstar

    Joined:
    Jul 2, 2010
    Posts:
    186
    Just cycle through a list or array of grenade prefabs. For example (in c# sorry, I dont do UnityScript)

    Code (csharp):
    1.  
    2. public List<Transform> m_grenadePrefabs;    // put your grenade prefabs in here
    3. public int m_currentGrenadeIndex = 0;           // index of currently selected grenade
    4.  
    5. void Update()
    6. {
    7.   if(Input.GetKeyDown ("h"))
    8.   {
    9.     ++m_currentGrenadeIndex;
    10.     if(m_currentGrenadeIndex >= m_grenadePrefabs.Count) { m_currentGrenadeIndex = 0; }
    11.   }
    12. }
    13.  
    14. void Throw()
    15. {
    16.   BulletPrefab = m_grenadePrefabs[m_currentGrenadeIndex];
    17.   .............
    18. }
    19.  
     
  3. The3DKnight

    The3DKnight

    Joined:
    Feb 8, 2011
    Posts:
    240
    Thanks for help, but can someone explain me in java what should i do, because i don't know C#
    Thanks again.
     
  4. nullstar

    nullstar

    Joined:
    Jul 2, 2010
    Posts:
    186
    Both c# and unityScript support lists and arrays so the concept is exactly the same in either language, its just the syntax that changes slightly. If you're not already familiar with arrays or lists I suggest you look up some tutorials on the subject since they are extremely useful.
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  6. The3DKnight

    The3DKnight

    Joined:
    Feb 8, 2011
    Posts:
    240
    Nullstar and Lefty if you or anybody gave me the script that i needed i wouldn't learn anything , but since you didn't i'm glade because i managed to understand it , and write it my self.So i wanted to thank you :D

    Here's the code if any body needs it

    Code (csharp):
    1. var BombPrefab : GameObject[];
    2. private var BombNum : int = 0;
    3. private var BombCount : int = 2;
    4.  
    5.  
    6.  
    7. public var ThrowFoce : float = 2500;
    8.  
    9. var Animation : AnimationClip;
    10.  
    11. var Gammo : int = 5;
    12.  
    13. //Hide/show objects
    14.  
    15. var Hands : GameObject;
    16. var Weapons : GameObject;
    17.  
    18.  
    19. function Update () {
    20.  
    21. if(Input.GetKeyDown ("h")){
    22.  
    23. BombNum++;
    24.  
    25. if(BombNum == BombCount) {
    26.  
    27.        BombNum = 0;
    28.  
    29.     }
    30.  
    31. }
    32.  
    33.  
    34.  
    35.  
    36.    if(Input.GetKeyDown ("g"))
    37.    {
    38.    if(Gammo > 0){
    39.    
    40.    Hands.animation.Play (Animation.name);
    41.    Hands.animation[Animation.name].speed = 1.4;
    42.    Throw();
    43.    Enable();
    44.    Weapons.active = false;
    45.    Hands.active = true;
    46.    }
    47.    }
    48.     if(!Hands.animation.IsPlaying(Animation.name))
    49.    {
    50.     Weapons.active = true;
    51.     Hands.active = false;
    52.  
    53.    }
    54.  
    55.    }
    56.    
    57. function Enable ()
    58. {
    59.  
    60.  if(!Hands.animation.IsPlaying(Animation.name))
    61.    {
    62.     Weapons.active = true;
    63.     Hands.active = false;
    64.    
    65.    }
    66.  
    67. }
    68.    
    69.    
    70. function Throw ()
    71.  
    72. {
    73. Gammo -= 1;
    74. yield WaitForSeconds (0.1);
    75. var BombCreate = Instantiate (BombPrefab[BombNum], GameObject.Find("SpawnPoint").transform.position, GameObject.Find("SpawnPoint").transform.rotation );
    76.           BombCreate.rigidbody.AddForce(transform.forward * ThrowFoce);
    77.  
    78.  
    79. }  
    80.  
     
  7. nullstar

    nullstar

    Joined:
    Jul 2, 2010
    Posts:
    186
    Nice work 3DKnight, its a good feeling isn't it.