Search Unity

Controller Trigger Help

Discussion in 'Scripting' started by JacksonTheXtremeGamer, Mar 27, 2020.

  1. JacksonTheXtremeGamer

    JacksonTheXtremeGamer

    Joined:
    Jun 15, 2019
    Posts:
    108
    I want to use the right trigger on the controller as the fire button. But how can I script it to where it doesn't mess up the mouse and keyboard controls? Here is the script for the cannon:
    Code (CSharp):
    1.     void Update()
    2.     {
    3.         if (Input.GetButtonDown("Fire1"))
    4.         {
    5.             Reload.ammoCount -= 1;
    6.             sound.Play();
    7.             GameObject bulletObject = Instantiate(bulletPrefab);
    8.             Rigidbody rb = bulletObject.GetComponent<Rigidbody>();
    9.             bulletObject.transform.position = playerTarget.transform.position + playerTarget.transform.forward;
    10.             bulletObject.transform.forward = playerTarget.transform.forward;
    11.             rb.AddForce(transform.forward * shootForce, ForceMode.VelocityChange);
    12.         }
    13.     }
    One more thing: The button is supposed to do single-fire shots. Any help is appreciated.
     
    Last edited: Mar 27, 2020
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,692
    With the old Unity InputManager, just make a map to bring in whatever joystick axis or button that the trigger is connected to so that it comes in on "Fire1"

    If that is difficult across varied controllers, it may be worthwhile to purchase a controller remap asset from the store, but I haven't used any of them so I cannot recommend one in particular.

    To make it only squeeze off one shot, you need to keep track of if the fire condition is true, and only fire a shot when it goes from false to true on any given frame.
     
  3. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    The new Input System covers this. Automatically switching between control schemes (keyboard/mouse to gamepad.) It can also handle separating UI input from gameplay input. The biggest issue I have with it is that its in preview, and a recent update broke how buttons worked. It was easy to fix what broke and was a change in the right direction, but that's what happens when you deal with something in preview.

    You can check out the asset store like @Kurt-Dekker posted. These should work like the new system, but more seasoned, hopefully more features and maybe more user friendly.

    Or do it yourself using the old system. Which treats triggers as an axis only, so you need to make it work like a button.
    Had this code in my current project until I switched over to the new Input System.
    Code (CSharp):
    1. private bool lastTrigger = false;
    2.  
    3. private void Update()
    4. {
    5.         float fireAxis = Input.GetAxisRaw("Fire1");
    6.         bool fire = Input.GetButtonDown("Fire1") || (fireAxis > 0 && !lastTrigger);
    7.  
    8.         if (fire)
    9.         {
    10.             //Fire here
    11.         }
    12.  
    13.         if (fireAxis == 0)
    14.         {
    15.             lastTrigger = false;
    16.         }
    17.         else
    18.         {
    19.             lastTrigger = true;
    20.         }
    21. }
     
  4. JacksonTheXtremeGamer

    JacksonTheXtremeGamer

    Joined:
    Jun 15, 2019
    Posts:
    108
    I am using the old system as a matter of fact. This is just what the doctor ordered. I’ll give it a shot.