Search Unity

Double shot...

Discussion in 'Scripting' started by ramunda, Aug 3, 2015.

  1. ramunda

    ramunda

    Joined:
    Jun 8, 2015
    Posts:
    28
    Hi all , i have a doubt...i have this scrip in a empty gameobject for firing a bullet(rigidbody) its works fine, but if i duplicate de game object an move it beside the first gameobject like a 2 Guns shotgun....not fires well.

    One single fires excelent...2..not firen nothing or firing wrong?

    What can be?
    Thanks in advance.

    The script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class disparosyrecargosnave : MonoBehaviour {
    4. public Rigidbody Bala;
    5. public float Velocidad = 80;
    6. public float municion = 10;
    7. public Animation reload;
    8. float municionderecargo = 10;
    9. public float municiontotal = 200;
    10. ///public static float municiontotal = 150;
    11. public disparosyrecargos2 MyScriptShoot;
    12. public float Conteo = 0;
    13. public GUIText guimuni;
    14. public GUIText guitotal;
    15. public GameObject destello;
    16. //public GameObject manoanim;
    17. public GameObject expulsabalas;
    18. public AudioClip recarga;
    19. public AudioClip gunShot;
    20. public AudioClip vacio;
    21. void Start (){
    22.  
    23. destello.active = false;
    24. }
    25.  
    26. void Update (){
    27.  
    28. guimuni.text = "Balas " + municion.ToString();
    29. guitotal.text = "Total Balas " + municiontotal.ToString();
    30.  
    31.      if (Input.GetButton("Fire1")){
    32.     //if(Input.GetMouseButtonDown(0)){  
    33.           Conteo = Conteo + 1;
    34.          
    35.      if(Conteo > 15){
    36.    
    37.       Conteo = 0;
    38. }  
    39.      if(Conteo == 15 && municion > 0 && municiontotal >= 0){
    40.            
    41.          
    42.         Rigidbody clone;
    43.         clone=Instantiate(Bala, transform.position, transform.rotation) as Rigidbody;
    44.         clone.velocity = transform.TransformDirection(Vector3.forward * 400);
    45.         municion -= 1;
    46.            Destroy (clone,1.0f);
    47.         GetComponent<AudioSource>().PlayOneShot(gunShot);
    48.        
    49. }
    50. }
    51.     if(Input.GetKeyDown("r") && municiontotal > 0 && !Input.GetMouseButton(0) && !Input.GetMouseButton(1)){
    52.     reload.Play("pistolamueve");
    53.     municion = 0;
    54.     Invoke ("recargando",2);
    55.      AudioSource.PlayClipAtPoint(recarga, transform.position, 1);
    56.  
    57. }
    58.  
    59.     if(Input.GetButton("Fire1")){
    60.    
    61.     destello.active = true;
    62. }
    63.     else
    64.     if(Input.GetMouseButtonUp(0)){
    65.     destello.active = false;
    66. }  
    67.     if (municion <= 0){
    68.     if(Input.GetButton("Fire1")){
    69.     expulsabalas.SetActive(false);
    70.     AudioSource.PlayClipAtPoint(vacio, transform.position, 1);
    71.     destello.active = false;
    72. }  
    73. }
    74. }
    75. void recargando (){
    76.    
    77.    
    78.     expulsabalas.SetActive(true);
    79.     if(municiontotal > 0){
    80.     municiontotal -= municionderecargo;
    81.     municion += municionderecargo;
    82.  
    83. }
    84.     if (municion > municionderecargo){
    85.     municion = municionderecargo;
    86. }
    87. }
    88. }
     
  2. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    I can't read the script you posted because the references being foreign words removes all sense of context from them, but I do see that you're instantiating a rigidbody (a component) and not a GameObject. This likely isn't going to work how you think it is. I would suggest making a GameObject with the rigidbody component attached (the bullet), and then making it a prefab. Drag your "bullet prefab" into your gun prefab as a public member and then be sure to "apply", then in your script instantiate the whole bullet object, not just one script. The rigidbody needs a GameObject to move, after all.
     
  3. ramunda

    ramunda

    Joined:
    Jun 8, 2015
    Posts:
    28
    Thanks for the reply Lysander, i was doint like you said, first atached the rigidbody i make a prefab and then dra it in to the ispector but also dont works :(