i was making simple 2d game from tutorial and I thought I would try to make it mobile,
I need help how to write script for the button; if i click the button it will be read as up key on
keyboard, or any key on keyboard (1 button per 1 key) i have controls for they keys and i don’t want to write everything from beginning (well i cannot write cause i have no idea what i’m doing)
You say you have controls for keyboard input already. How does that look?
If it is somethink like this
private void Update() {
if(Input.GetKeyDown(KeyCode.A)) {
PressA();
}
}
private void PressA() {
// handle input
}
then your button could just call PressA()
as well and the A-key and the button would have the same effect. Is this what you want?
if (Input.GetKeyDown(KeyCode.UpArrow) && transform.position.y < maxY) {
camAnim.SetTrigger("shake");
Instantiate(moveEffect, transform.position, Quaternion.identity);
targetPos = new Vector2(transform.position.x, transform.position.y + increment);
} else if (Input.GetKeyDown(KeyCode.DownArrow) && transform.position.y > minY) {
camAnim.SetTrigger("shake");
Instantiate(moveEffect, transform.position, Quaternion.identity);
targetPos = new Vector2(transform.position.x, transform.position.y - increment);
i would want something like if i clickbutton on screen it would have same effect as clicking button on keybord
You have to rewrite this like so:
if (Input.GetKeyDown(KeyCode.UpArrow))
{
PressUp();
}
private void PressUp()
{
if(transform.position.y >= maxY) return;
camAnim.SetTrigger("shake");
Instantiate(moveEffect, transform.position, Quaternion.identity);
targetPos = new Vector2(transform.position.x, transform.position.y + increment);
}
and then your button can also call the PressUp() function.