CarController using UI Buttons

Hi everyone!

I have been working/messing with Unity for two years, but I have reached a roadblock in my current “experiment” that I can’t find how to resolve.

Following Jimmy Vega’s tutorial about doing a racing game, I’m trying to build a game using the car controllers from StandarAssets. The trick is that I want the car to be controlled not but the physical keyboard, but using on-screen UI buttons since it is mainly intended for a kid which has a high degree of disability and can only use its computer with eye-tracking devices like Tobii.
I have tried attaching the AxisTouchButton script (also from the StandarAssets) to the buttons so they provide the input for the Axes, and this technically work (a simple Log of “m_Axis.GetValue” shows how the values change) but these inputs do not reach CarController as the keyboard do, so the player car doesn’t move :frowning:

I would thank any tip or help about making this “connection”. The axis value change when pressing the UI buttons, so I don’t get why the script for driving the car do not feed on them as when insrted via keyboard.

Many thanks in advance!

You would probably need to check the part of the code where the inputs are read in your car controller, then change it to use the inputs you provide with your AxisTouchButton script.

Try to find the part in the code first and then show it here, maybe someone can give you a nudge in the right direction.

Hi again!

Thanks for the advice!

The code is the default one in CarUserControl, found on the Standard Assets pack:

        private void FixedUpdate()
        {
            // pass the input to the car!
            float h = CrossPlatformInputManager.GetAxis("Horizontal");
            float v = CrossPlatformInputManager.GetAxis("Vertical");
#if !MOBILE_INPUT
            float handbrake = CrossPlatformInputManager.GetAxis("Jump");
            m_Car.Move(h, v, v, handbrake);

            Debug.Log(h +" " +v + " " + handbrake);
#else
            m_Car.Move(h, v, v, 0f);
#endif
        }

This calls the Move function on CarController, also from the Standard Assets pack:

public void Move(float steering, float accel, float footbrake, float handbrake)
        {
            for (int i = 0; i < 4; i++)
            {
                Quaternion quat;
                Vector3 position;
                m_WheelColliders[i].GetWorldPose(out position, out quat);
                m_WheelMeshes[i].transform.position = position;
                m_WheelMeshes[i].transform.rotation = quat;
            }

            //clamp input values
            steering = Mathf.Clamp(steering, -1, 1);
            AccelInput = accel = Mathf.Clamp(accel, 0, 1);
            BrakeInput = footbrake = -1*Mathf.Clamp(footbrake, -1, 0);
            handbrake = Mathf.Clamp(handbrake, 0, 1);

            //Set the steer on the front wheels.
            //Assuming that wheels 0 and 1 are the front wheels.
            m_SteerAngle = steering*m_MaximumSteerAngle;
            m_WheelColliders[0].steerAngle = m_SteerAngle;
            m_WheelColliders[1].steerAngle = m_SteerAngle;

            SteerHelper();
            ApplyDrive(accel, footbrake);
            CapSpeed();

            //Set the handbrake.
            //Assuming that wheels 2 and 3 are the rear wheels.
            if (handbrake > 0f)
            {
                var hbTorque = handbrake*m_MaxHandbrakeTorque;
                m_WheelColliders[2].brakeTorque = hbTorque;
                m_WheelColliders[3].brakeTorque = hbTorque;
            }


            CalculateRevs();
            GearChanging();

            AddDownForce();
            CheckForWheelSpin();
            TractionControl();
        }

The Axis are mapped to the usual arrow keys/ AWSD controls, which work fine. What I (unsuccesfully) do is create a Panel with UIButtons simulating the physical keys, adding the AxisTouchButton script to them (once again, it comes on the Standard Assets).

I have watched examples and tutorials in which this kind of “experiment” work, with no need to modify the scripts using the GetAxis function so they recognize the UIButtons. Just attach AxisTouchButton to your on-screen buttons, tweak the params a bit, and voilà. Since the scripts for controlling the cars are default ones, I hesitate to modify anything on them (and I don’t see myself able to replicate a full functioning system of controlling cars with torque, steering, etc…)

Any tip or hint to unlock this problem would be of great help, this is just the last step to have at least a playable demo.

Thank you in advance!

Ok, looks like I found the issue (and it is a really dumb one :sweat_smile: ).

I was just testing my game as a PC/Mac/Linux standalone. I switched to iOS and the buttons began to work as intended :stuck_out_tongue:

Thank you very much!