Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Shots not firing

Discussion in '2D' started by m474, May 26, 2015.

  1. m474

    m474

    Joined:
    May 26, 2015
    Posts:
    2
    When I try to fire shots they only fall down...
    and also the shots come 3 at a time even though I set it to one every 0.5 second

    This probably looks stupid but I just don't know what I do wrong :(

    http://prntscr.com/79rwiy

    missile.js
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. function Start () {
    4.  
    5. }
    6.  
    7. function Update () {
    8.  
    9. }
    10.  
    11. function OnCollisionEnter2D(infoObjetToucher:Collision2D)
    12. {
    13.     if (infoObjetToucher.gameObject.name == "alien")
    14.     {
    15.         yield WaitForSeconds(0.1);
    16.         infoObjetToucher.gameObject.SetActive(false);
    17.     }
    18. }
    joueur.js
    Code (JavaScript):
    1. #pragma strict
    2. var forceX:float;
    3. var vitesseMax:float;
    4. var missile:GameObject;
    5. var speed_mis:float;
    6.  
    7.  
    8. function Start () {
    9.  
    10. }
    11.  
    12. function Update () {
    13.    
    14.     if(Input.GetButtonDown("Fire2")){
    15.         InvokeRepeating("creationObjet", 0, 0.5);
    16.     }
    17.  
    18.     if(Input.GetButtonUp("Fire2")){
    19.         CancelInvoke();
    20.     }
    21. }
    22.  
    23. function OnBecameInvisible(){
    24.     Destroy(gameObject);
    25. }
    26.  
    27. function OnMouseDown(){
    28.     Destroy(gameObject);
    29. }
    30.  
    31.  
    32.  
    33. function FixedUpdate() {
    34.  
    35.     var deplacementX = Input.GetAxis("Horizontal") * forceX;
    36.    
    37.     if( (rigidbody2D.velocity.x > -vitesseMax) && (rigidbody2D.velocity.x < vitesseMax))
    38.     {
    39.         rigidbody2D.AddForce(Vector2(deplacementX,0));
    40.     }
    41.    
    42.     if (deplacementX > 0) transform.localScale.x = 1;
    43.     if (deplacementX < 0) transform.localScale.x = -1;
    44. }
    45.  
    46.  
    47. function creationObjet(){
    48.     var objet = Instantiate(missile, Vector3(0, 1, 0), Quaternion.identity);
    49.     objet.SetActive(true);
    50.     objet.rigidbody.AddRelativeForce(transform.up * Time.deltaTime * speed_mis);
    51.  
    52. }
    http://prntscr.com/79s2rr

    Thanks in advance :)
     
  2. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    969
    Set the force or velocity in a script on the missile instead of just once when you create it if you don't want it to fall down. If you do want it to fall down, but after a longer time, try using ForceMode.Impulse.

    I can't see why this would happen. Have you added the script to multiple gameobjects? Or does your missile prefab have 3 in it?
     
  3. m474

    m474

    Joined:
    May 26, 2015
    Posts:
    2
    I've set the shots gravity to -1 and now it works :p
    I've also moved the Instantiate into the if getbuttondown and now the 3 shots thing is solved