How could I transform keyboard input into touch input?
Hello! I'm making a racing game and I wanted to port it over to mobile, but I'm having a lot of trouble trying to figure out the best way to do it. Initially, I thought that simulating a keypress with a UI button would work well but thanks to the help of @Hellium I'm now trying to figure out another way to port this game over. Here is the current code for the BaseInput: using UnityEngine; namespace KartGame.KartSystems { public struct InputData { public bool Accelerate; public bool Brake; public float TurnInput; }
public interface IInput
{
InputData GenerateInput();
}
public abstract class BaseInput : MonoBehaviour, IInput
{
/// <summary>
/// Override this function to generate an XY input that can be used to steer and control the car.
/// </summary>
public abstract InputData GenerateInput();
}
}
And here is the keyboard input script, these "horizontal", "accelerate" and "brake" buttons are all referencing to Unity's input manager, as you can see in the image attached!
using UnityEngine;
namespace KartGame.KartSystems {
public class KeyboardInput : BaseInput
{
public string TurnInputName = "Horizontal";
public string AccelerateButtonName = "Accelerate";
public string BrakeButtonName = "Brake";
public override InputData GenerateInput() {
return new InputData
{
Accelerate = Input.GetButton(AccelerateButtonName),
Brake = Input.GetButton(BrakeButtonName),
TurnInput = Input.GetAxis("Horizontal")
};
}
}
}
What would be the best way to change this keyboard input method to a mobile-compatible one? Ideally, I would like a joystick for movement but left and right buttons would also be fine. Thanks again @Hellium for the help with the previous incorrect question that I've made!
Answer by Hellium · Jan 27 at 09:06 AM
The solution I suggested in the other question works perfectly fine
UiInput.cs
To be attached to an empty gameObject
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UiInput : BaseInput
{
private bool accelerate;
private bool brake;
private float turn;
// Called by OnPointerDown event from Even Trigger attached to UI object
// https://youtu.be/3NBYqPAA5Es?t=194
public void StartAccelerate() => accelerate = true;
// Called by OnPointerUp event from Even Trigger attached to UI object
// https://youtu.be/3NBYqPAA5Es?t=194
public void StopAccelerate() => accelerate = false;
public void StartBrake() => brake = true;
public void StopBrake() => brake = false;
public void StartTurnLeft() => turn = Mathf.Max(turn - 1, -1);
public void StopTurnLeft() => turn = Mathf.Min(turn + 1, 1);
public void StartTurnRight() => turn = Mathf.Min(turn + 1, 1);
public void StopTurnRight() => turn = Mathf.Max(turn - 1, -1);
public override InputData GenerateInput()
{
return new InputData
{
Accelerate = accelerate,
Brake = brake,
TurnInput = turn
};
}
}
Then, for every UI button, attach an event trigger and provide the appropriate function
Example for the accelerate button:
I've done everything exactly as you specified and it is still not working, the buttons aren't doing anything when pressed, what am I doing wrong? What information could I provide you to help you troubleshoot this?
Your answer
Follow this Question
Related Questions
Porting to mobile how to assign input manager axes to GUIButtons. 0 Answers
Have a UI Button do what my Input.GetButtonDown("Use") does? 0 Answers
New Input System - On-screen stick only recieves initial input 0 Answers
CrossPlatformInput Detect Multiple Input Headache 1 Answer
Blender Inputs to Unity Inputs 1 Answer