- Home /
how to translate key input controls to touch controls? (For Mobile gaming)
ive been fiddling around for weeks now trying everything i can think of to make my controls work from keyboard input to gui input but i havent to get them to function right so im going to go back to square 1 and reach out and see if anyone can lead me in the right direction
i have these 2 scripts for controlling my player via keyboard input
this is for attacking an enemy
using UnityEngine;
using System.Collections;
public class attacks : MonoBehaviour {
public int speed = 2;
private int attack = 0;
Animator anim;
void Start ()
{
anim = GetComponent<Animator>();
}
void Update ()
{
attack = 0;
speed = 0;
if(Input.GetKeyDown(KeyCode.Q))
{
attack = 1;
}
if(Input.GetKeyDown(KeyCode.W))
{
attack = 2;
}
if(Input.GetKeyDown(KeyCode.E))
{
attack = 3;
}
if(Input.GetKeyDown(KeyCode.R))
{
attack = 4;
}
if(Input.GetKey(KeyCode.S))
{
attack = -1;
}
anim.SetInteger("attack", attack);
}
}
each "attack" value corresponds to an animation parameter to call an animation
this script is for moving left and right
using UnityEngine;
using System.Collections;
public class playermovement : MonoBehaviour
{
public int speed = 2;
private int direction = 0;
Animator anim;
void Start ()
{
anim = GetComponent<Animator>();
}
void Update ()
{
direction = 0;
if(Input.GetKey(KeyCode.D))
{
transform.Translate(Vector2.right * speed * Time.deltaTime);
direction = 1;
}
if(Input.GetKey(KeyCode.A))
{
transform.Translate(-Vector2.right * speed * Time.deltaTime);
direction = -1;
}
if(Input.GetKey(KeyCode.D)
&& Input.GetKey(KeyCode.A))
{
direction = 0;
}
anim.SetInteger("speed", direction);
}
}
the "direction" values correspond to left or right direction in the animator
i don't want this done totally for me, i want to know how to do it and be able to recreate this in the future, but i would appreciate it if someone could give me a general idea of what functions i should be using
by the way, my GUI buttons were made using unity 4.6 with the canvas Button UI feature if that makes a difference on how it should be done
Answer by Freaking-Pingo · Sep 24, 2014 at 12:21 AM
Okay, I'll provide you with a directional hint then. In the new GUI system, you can see on the Button component, that they have a "On Click()" box at the bottom of the Button component. This box is capable of manipulating variables or even execute methods in the custom scripts you are making. However, your current code isn't really setup for this yet, because all your input logic is handled within the Update loop. Instead I'll suggest, for starters, you drop each logical input into each own separate method. So, for example:
public void MoveRight()
{
//Add code, that enables moving right.
}
If you know select your GUI button, which corresponds to "Going Right", you click the "+" sign in the OnClick box, drag the GameObject which contains the playermovement script into the object field, find the corresponding function you just created "playermovement > MoveRight", and wola, when hitting the button the MoveRight method should now be executed instead.
so will i need to make 7 separate scripts for each motion then?
Not seven seperate scripts, but seven different methods within the same script.
public class playermovement : $$anonymous$$onoBehaviour
{
public void $$anonymous$$oveRight()
{
// Add logic
}
public void $$anonymous$$oveLeft()
{
// Add logic
}
public void $$anonymous$$oveUp()
{
// Add logic
}
// And so forth...
}
This solution, however, will properly not fulfill what you currently need, but it is a step in the direction. Each of these method calls are only executed once, as you push the designated GUI button, so you will somehow need to find an approach that allows you for registering either a continuos touch, or a On Touch / Leave Touch.
Your answer
