- Home /
Android touch input
I want a simple touch input on the android, just when i touch the screen(anywhere) that something happens, anyone got any idea on how to do this?
Answer by Henrique Vilela · May 24, 2011 at 12:16 PM
void Update() {
if (Input.touchCount == 1) {
// Do something
}
}
Does this also work for IOS? Yes I know this is a old question but I'm very new to Input since I'm trying to make my game from windows controls to Ipod/Ipad controls. I've been working in windows this whole time. Anyways I tried using that piece of code for this:
if (grounded && Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Space)) {
anim.SetBool ("Ground", false);
rigidbody2D.AddForce (new Vector2 (0, jumpForce));
By just replacing the if statement with the one you posted, but nothing seems to work(the Player is not jumping). I'm testing this on Unity by the way, not some simulator or an actual device.
Answer by MDennis · May 24, 2011 at 09:04 AM
http://unity3d.com/support/documentation/Manual/Input.html#AndroidInput
that should get you started
i tried this: var Speed = 8 ; var rotateSpeed = 40; var start = false;
var distanceCheck = 0;
var Target : Transform ;
function Update () { distanceCheck = Vector3.Distance(Target.position, transform.position); if(start) { transform.Translate(Vector3.right Speed Time.deltaTime); for (var touch : Touch in Input.touches) { if (touch.phase == TouchPhase.Began) { transform.Rotate(Vector3.forward rotateSpeed Time.deltaTime); } else { transform.Rotate(Vector3.forward -rotateSpeed Time.deltaTime); } } } }
But when i test on the android, there is no movement happening at all, i used the input for space for testing and then everything worked but now not anymore,, anyone has any idea??
ps: i just need that when i press anywhere in the screen
If you just want to be able to press anywhere on the screen, i will tell you what i did for the iPhone. Which you can easily do for Android.
On the main camera create a child, which will be a plane. Remove the mesh and mesh collider from it and place a plane collider on it. Now position it so what ever the camera is facing it is looking through that plane as if it is a window. $$anonymous$$ake sure it is also trigger on by the way. Now place a script onto the plane so that whenever the player presses the screen it interacts with the plane and something happens.
So for example on my script;
var THEGA$$anonymous$$EOBJECTYOUWANTSO$$anonymous$$ETHINGTOHAPPEN: GameObject;
function FixedUpdate() { if(Input.touchCount >=1) { var touch: Touch = Input.touches[0]; if(touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled) { do what you wanted here } else { stop what you wanted here } } }
hopefully that will help now
Answer by mivi06 · Jan 15, 2012 at 05:46 PM
i have unity 3.4 with license full! i build it .apk it opend on my phone but doesnt function, becz i neead a script for touch. my script is whenever i click with the mouse the box disappears. how can i change the mouse click to finger touch? this is my script
function OnMouseDown(){
//if cube's texture isn't locked texture then it loads level number which we gave it in editor
if(renderer.material.mainTexture != lock){
Application.LoadLevel(loadlevel);
}
}
Please Help!
I figured it out eventually, don't know if you found it yourself already, but if the control is when you touch anywhere on the screen something happens, then you'll need this code:
if(Input.TouchCount == 1) { //do something }
if it doesn't work change the capitals of TouchCount to touchCount or Touchcount
thkx raynond i used dave's method and it workes like a charm! rlly thank u!
Your answer
![](https://koobas.hobune.stream/wayback/20220612083904im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Is touch began guaranteed? 0 Answers
Android Multi-Touch Issue 2 Answers
2D Camera (Android, Iphone) 1 Answer
Create on Gui controls for Mobile Devices (Android) 1 Answer
Best Way to make Swipe to Look function via touch. (Android) 0 Answers