- Home /
Make a character jump when user taps the screen of an android device
Hi, I have been trying all sorts of code to make my character jump by tapping the screen. This is for an android device. I have no problems making everything work correctly in Xcode, I just an illogical inability to get things working for android devices.
Thanks in advance
where are you facing troubles ,with recognizing the touches ? or adding the code to manage the physical action of the gameObject (adding some force in Y axis if it is pointing up) ?
Answer by Graphicated · Apr 09, 2012 at 04:37 PM
this will manage each touch (up to five fingers depending on the device). first off all there are 5 situations a Touchphase will face see here : 125Link125
so you have to decide when the action should happen , when beginning touch, or .....
here is a small example that will run whatever you want when the touch begins and if the touch not over five fingers or the user stopped touching:
void Update()
{
int fingerCount = 0;
foreach (Touch touch in Input.touches) {
if (touch.phase == TouchPhase.Began && touch.phase != TouchPhase.Canceled)
{
fingerCount++;
}
}
}
if (fingerCount > 0)
{
// manage the Vector or Force Manipulation here of the Player Prefab
}
Another Example where you manage if it is only 1 finger touching or 2 and so on:
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
void Update() {
int fingerCount = 0;
foreach (Touch touch in Input.touches) {
if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
fingerCount++;
}
if (fingerCount == 1)
{
print("User has " + fingerCount + " finger(s) touching the screen");
}else if (fingerCount ==2)
{
// do another thing if 2 fingers
}
}
}
hope this will help you to start solving your problem.
I have it working now, but am a little confused. Why does if (fingerCount > 0) dir.y = -Input.acceleration.x*.5; transform.position.y += dir.y; }
work, but
if (fingerCount > 0) rigidbody.AddRelativeForce (Vector3.up * jumpStrength); }
does not work?
Part of the code is not shown here.
are you getting any error ? i didnt try this method before
i think it is better to start a new question, so everyone can find it ,, it is a diffrent matter now ^^
Your answer
![](https://koobas.hobune.stream/wayback/20220613070054im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Jumping irregularities 1 Answer
How To Create Android 3d Game Jump Button 2 Answers
Constrain Total Number of Jumps 1 Answer
Jump Script for 2D (JavaScript) 0 Answers
Jumping with touch input. 0 Answers