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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Button on off new input system

Discussion in 'Scripting' started by Meow_meow23, Apr 22, 2022.

  1. Meow_meow23

    Meow_meow23

    Joined:
    Apr 22, 2022
    Posts:
    2
    Hello, i need help in coding a script for button on off using new input system for android. When I press the ui button my character crouches, and when I press the same button my character stops crouching. If it's not possible using input system, simple coding is also fine.
     
  2. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,064
    You can use the Button onClick event to toggle a boolean whether your character has to crouch or not.
     
  3. Meow_meow23

    Meow_meow23

    Joined:
    Apr 22, 2022
    Posts:
    2
    Can u pls write the code for me? I'm new to coding so idk much
     
  4. SourGummi

    SourGummi

    Joined:
    Nov 14, 2021
    Posts:
    96
    you wont learn anything that way.

    this will give you a toggle variable.
    Code (CSharp):
    1. private bool button_state_toggled = false;
    This will get you started
    Code (CSharp):
    1. // this method will be used to change the state of the button
    2. private void ChangeState()
    3. {
    4.     // change value
    5. }
    6.  
    7. // this method can be used for conditional stuff if button is on/off
    8. private bool GetButtonToggleState()
    9. {
    10.     return button_toggle_state;
    11. }
     
  5. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,064
    Create uGUI
    Button

    Create a component that controls the crouch of your character animator
    if you don't know how to make your character crouch, start with that first.
    Make the method that toggles the boolean public
    serialize the onClick UnityEvent in the inspector to call the method


    There's a lot of stuff you can learn at:
    https://learn.unity.com

    Documentation on Button
    https://docs.unity3d.com/Packages/com.unity.ugui@1.0/manual/script-Button.html
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,954
    The purpose of this forum is to assist people who are ready to learn by doing, and who are unafraid to get their hands dirty learning how to code, particularly in the context of Unity3D.

    This assumes you have at least written and studied some code and have run into some kind of issue.

    If you haven't even started yet, go check out some Youtube videos for whatever game design you have in mind. There are already many examples of the individual parts and concepts involved, as there is nothing truly new under the sun.

    If you just want someone to do it for you, you need go to one of these places:

    https://forum.unity.com/forums/commercial-job-offering.49/

    https://forum.unity.com/forums/non-commercial-collaboration.17/

    https://livehelp.unity.com/?keywords=&page=1&searchTypes=lessons

    Otherwise, be like this awesome guy and make yourself into an actual game developer:

    Imphenzia: How Did I Learn To Make Games:

     
    LunarBullet and ahmedpro1112 like this.