Search Unity

programming problem

Discussion in 'Scripting' started by BBG_dev, May 28, 2015.

  1. BBG_dev

    BBG_dev

    Joined:
    May 10, 2015
    Posts:
    6
    Code (JavaScript):
    1. import UnityEngine.Audio;
    2.  
    3. static var ammo = 9;
    4. static var maxAmmo = 9;
    5. var key : String = "mouse 0";
    6. var bullet : Rigidbody;
    7. var speed : float = 1000;
    8. var bulletSound : AudioClip;
    9. var soundofgun : GameObject;
    10. var gun : GameObject;
    11. var ani : Animation;
    12.  
    13. function Start () {
    14.  
    15. ani = GetComponent ("Animation");
    16.  
    17. }
    18.  
    19. function Update () {
    20.     if(Input.GetKeyDown(key)){
    21.        
    22.         if(ammo > 0){
    23.             shoot();
    24.         }
    25.         }
    26.         if(Input.GetKeyDown("r")){
    27.        
    28.         ammo = 9;
    29.         ani.Play("reload");
    30.     }
    31. }
    32. function shoot(){
    33.     var bullet1 : Rigidbody = Instantiate(bullet, transform.position, transform.rotation);
    34.     bullet1.AddForce(transform.forward * speed);
    35.     soundofgun.GetComponent.<AudioSource>().Play();
    36.     ammo --;
    37. }
    38.  
    39. function OnGUI(){
    40.     GUI.Label(Rect(10, 10, 500, 500), "Gun ammo "+ammo);
    41. }
    it doesnt play the animation, please help

    Note: animation is atached and its marked as a legacy animation, no controller
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Input.GetKeyDown doesn't work on mouse buttons, just keyboard. You're looking for Input.GetMouseButtonDown, or Input.GetButtonDown if you want to set it up in the input manager.

    It's case sensitive as well. "mouse 0" is not "Mouse 0".

    I'd also recommend using the KeyCode version of GetKey instead of the string version - it's harder to make small mistakes.

    Also, in the future, please be more descriptive with your subject line. Every post here is a programming problem.
     
    JoeStrout likes this.