Search Unity

Shooting Script

Discussion in 'PSM' started by dotmatrixgame, Oct 19, 2014.

  1. dotmatrixgame

    dotmatrixgame

    Joined:
    Aug 9, 2014
    Posts:
    3
    I have the most basic shooting script:
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var theBullet : Rigidbody;
    4. var Speed = 20;
    5.  
    6. function Update () {
    7.     if (Input.GetKey(KeyCode.Joystick1Button5))
    8.     {
    9.         var clone = Instantiate(theBullet, transform.position, transform.rotation);
    10.         clone.velocity = transform.TransformDirection(Vector3(0, 0, Speed));
    11.        
    12.         Destroy (clone.gameObject, 1);
    13.     }
    14. }
    When I have it to left mouse click for testing, it shoots one bullet per click.
    I have it set to R, and if I hold it down it will constantly shoot. Anyway to fix this?
     
  2. jesusluvsyooh

    jesusluvsyooh

    Joined:
    Jan 10, 2012
    Posts:
    377
    "Input.GetKeyDown" what you should use
    "Input.GetKeyUp" good for plasma cannon/charging type weapon

    :) Or use a boolean and some Yield WaitForSeconds(); perhaps, many ways you could do it.
     
  3. dotmatrixgame

    dotmatrixgame

    Joined:
    Aug 9, 2014
    Posts:
    3
    Ah, thanks!