- Home /
Problem is not reproducible or outdated
Unity : Adapting mouse based game to touch controls
I developed a basic game in which the camera can be moved across the map using the WASD keys and i wanted to adapt the basic button actions to work on a touch screen , but I am unsure how to start.
Heres the c# code i currently have for moving the camera
if (Input.anyKey && allowInput)
{
moved = false;
if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W)) { moved = true; Translate(Vector3.forward * Time.deltaTime * speed); }
if (Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S)) { moved = true; Translate(Vector3.back * Time.deltaTime * speed);}
if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A)) { moved = true; Translate(Vector3.left * Time.deltaTime * speed);}
if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D)) { moved = true; Translate(Vector3.right * Time.deltaTime * speed); }
if (OnCamManuallyMoved != null && moved)
{
Vector3 pos = tr.position;
if (pos.x < min_xz.x) pos.x = min_xz.x;
if (pos.x > max_xz.x) pos.x = max_xz.x;
if (pos.z < min_xz.y) pos.z = min_xz.y;
if (pos.z > max_xz.y) pos.z = max_xz.y;
tr.position = pos;
OnCamManuallyMoved(); // call callback
}
}
Answer by tanoshimi · Oct 02, 2013 at 06:30 AM
This is a question of game design, not one of coding.
A good place to start would be to write down or draw, with a pen and paper, how you expect to map key presses to a touch screen. Do you want to tap areas of the screen to make the camera move there? Do you want a virtual keyboard to appear on screen showing U/D/L/R keys, or perhaps a virtual joystick? Many touch devices also have accelerometers and gyros - do you want to tilt the device to move the camera? Or do you want to tap at the edges of the screen?
All these are possible, but nobody here can decide which one you want. Until you get the design sorted out, forget about the code. If you get stuck implementing your chosen solution, come back and ask a specific question.
Answer by Ranger-Ori · Oct 02, 2013 at 07:18 AM
Well, you basically need to learn about the touch control system from scratch, not only by design, but understanding how it actually works, from a simple touch, to an object movement.
Import the "Standard Assets (Mobile)" built-in asset you have in your unity 3d. Check the scenes and examples, if you don't manage to understand what's going on, then go to youtube or google and start to read about the touch system.
http://docs.unity3d.com/Documentation/Manual/iphone-GettingStarted.html http://docs.unity3d.com/Documentation/ScriptReference/Touch.html
Good luck!
Follow this Question
Related Questions
Unity's Tech demo Procedural help? 0 Answers
Rotating an Object while Moving 0 Answers
how to work my game 0 Answers
Object rotating around object at two levels 1 Answer
Translate speed keeps changing 1 Answer