- Home /
Touch buttons to replace Input GetKey KeyCode
Hey guys, I really need help here, I'm creating a constantly moving 2D game, it's functioning fully in unity and for pc, but I've made it for mobile devices, I've looked everywhere online and around here for an answer but haven't found one that works in my situation
here is the segment of my code that contains button presses:
if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
{
rigidbody.AddRelativeTorque(0,0,1.5f);
}
if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
{
rigidbody.AddRelativeTorque(0,0,-1.5f);
}
I have made a couple of sprites that I intend to replace the keyboard left and right for touch inputs, I just need to know how to code it using my current code.
Any ideas/help would be greatly appreciated!
Thanks.
Answer by fifthknotch · Mar 09, 2014 at 05:25 AM
First thumbs up for one of the best written questions I've had to privilege to answer. You need to study the Unity docs for mobile input. That is found here:
http://docs.unity3d.com/Documentation/Manual/Input.html
Particularly check out GUI hittest in the script reference.
here is a code snippet of how i used this to make my game pause when the user pressed the pause "button" (it was actually just a texture that represented 2D space input could be read in as clicking pause):
#pragma strict
var pauseButton : GUITexture;
var isPaused = false;
var timer : float = 0.0;
function Start () {
isPaused = false;
}
function Update () {
// This starts a timer
if (timer <= 1.0)
timer = timer + .1;
var count : int = Input.touchCount;
for (var touch : Touch in Input.touches) {
if (pauseButton.HitTest( touch.position )) {
// This checks that timer to make sure it has given the user enough time
// to lift their finger before it cycles through the remaining code again.
if (timer >= 1.0) {
if (isPaused) {
Time.timeScale = 1;
isPaused = false;
}
else {
Time.timeScale = 0;
isPaused = true;
}
timer=0.0;
}
}
}
}
Thanks for the response and I'm glad you liked how I asked my question.
The link you posted, I've been studying that page all day and I can't figure out how to use a sprite in the same context as I have my code currently in
I really appreciate the code you provided, that may come in handy to me later, creating a pause menu was next on my to-do-list.
So did this answer your question? Or do you have another?
Not exactly, I still can't figure out how to implement a touch control ins$$anonymous$$d of keyboard left and right, do you know how I might add it to my code?
I really appreciate your time to help me here
Do you want me to create your code for you? Or would you like to play around with the script I just gave you? I have it all worked out for what to change here and there in your code to get your input working, but I don't feel that that is the best way for me to help you.
This is the problem with me just writing your code for you: once you get the code I write to work, you won't understand how it worked or what I did to make it work. Part of learning is you need to understand why something works. Sometimes suffering over finding the solution to a problem is a good thing because you will always remember how you figured it out once you finally do. :) I can give you some code if you want, but I think you can figure this out...
I would really appreciate it if you gave me the code, believe me, I will definitely know why it works and will remember it, I've slaved over this problem for about 12 hours straight (no breaks), I think it will take me another 24 hours straight to figure it out by myself
It would be great if you could post the code
Your answer
Follow this Question
Related Questions
Touch Inputs Not Allowing Mutiple Presses 1 Answer
Android - touch a 3d object and something happens to it 1 Answer
Touch Input madness 2 Answers
Touch joystick tutorials ? 0 Answers
input touch android help 0 Answers