Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

3rd Axis goes rapid once held down. How do I fix this?

Discussion in 'General Discussion' started by TheHiper1010, Nov 27, 2014.

  1. TheHiper1010

    TheHiper1010

    Joined:
    Sep 16, 2014
    Posts:
    6
    I been using this Javascript for Raycast styled shooting. I also added controller support to the game but when I hold down LT on the Xbox 360 controller (Which is the button I set to for firing the gun) instead of the gun firing one bullet each time I press down LT, the gun just fires rapidly like a machine gun when I need it to fire like how handguns work in games. How do I fix this problem so the gun only shoots one bullet each time I press down LT?

    Incase you need to see the Raycast shooting script, here it is:

    Code (csharp):
    1. #pragma strict
    2.  
    3. var Effect : Transform;
    4. var TheDammage = 100;
    5.  
    6. function Update () {
    7.  
    8.     var hit : RaycastHit;
    9.     var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));
    10.  
    11.     if (Input.GetMouseButtonDown(0) || Input.GetAxis("Xbox360ControllerTriggers"))
    12.     {
    13.         if (Physics.Raycast (ray, hit, 100))
    14.         {
    15.             var particleClone = Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
    16.             Destroy(particleClone.gameObject, 2);
    17.             hit.transform.SendMessage("ApplyDammage", TheDammage, SendMessageOptions.DontRequireReceiver);
    18.             audio.Play();
    19.         }
    20.     }
    21.  
    22. }
     
  2. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    I formatted your code for you so the community can actually read your script code. Use the code tag in square brackets to do this.
     
  3. TheHiper1010

    TheHiper1010

    Joined:
    Sep 16, 2014
    Posts:
    6
    Okay thanks but do you have a solution to my question?
     
  4. CaoMengde777

    CaoMengde777

    Joined:
    Nov 5, 2013
    Posts:
    813
    you put a timer.. and make a rate of fire... you say if(timer >= 0.4 seconds (or whatever time)) canshoot = true .. if (canshoot == true) then Shoot, canshoot = false, and timer = 0

    or something like that

    without a timer, yeah.. youre saying it Shoots as long as button is held, every frame it shoots.