Search Unity

Input Manager Question (GetButtonDown)

Discussion in 'Scripting' started by cutelilbear, Jul 29, 2014.

  1. cutelilbear

    cutelilbear

    Joined:
    Jun 9, 2014
    Posts:
    5
    Hello,

    New to Unity here, Im currently trying to adjust a script i purchased and was hoping someone can help me out here.

    The current script contains "InputGetAxis" where if you press up or down it moves the car forward and backwards.

    ***************************************************************************************************************
    void FixedUpdate()
    {
    if (!Enabled)
    return;

    #region UserInput
    float vertical = Input.GetAxis("Vertical"); //Forward & Backward drive input
    float horizontal = Input.GetAxis("Horizontal"); //Angular velocity control input
    float space = Input.GetAxis("Jump"); //break control input
    #endregion

    if (vertical > 0)
    CurrentEngineAcceleration = Acceleration * vertical;
    else CurrentEngineAcceleration = (Acceleration * vertical) * BackwardAccelerationFactor;
    *****************************************************************************************************************

    What i need help is, is how do you change this code so that instead of pressing up and down on the keyboard, how do you change this so that when a player press a button with a NGUI button i created on the screen on the screen it moves the car forward and backwards?

    Thanks for the help if you can
     
  2. Patico

    Patico

    Joined:
    May 21, 2013
    Posts:
    886
    I think, you shold follow these steps:
    1. Move variables vertical, horizontal and space from FixedUpdate() method to the class level (make them as fields).
    2. Add them a setter methods or make them public
    3. Create a class for getting input from NGUI buttons, add a reference to first class, and change vertical, horizontal and space fields via setter methods when NGUI buttons will be pressed.