- Home /
MultiTouch Help(C#)
Hi Guys,
I am developing a app.
I have a script that splits the screen into 2 the left side moves the player and the right side makes the player jump. The problem is that when i move the player and i want to make the player jump at the same time the player doesnt jump. i understand that this is multitouch but i cant seem to find a way to fix this.
i want it so when i move the player and i want to make him jump as he is moving he jumps.
Script
using UnityEngine;
using System.Collections;
public class HalfScreen : MonoBehaviour {
public float JumpSpeed = 50f;
public bool grounded = true;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.touchCount > 0 )
{
var touch = Input.touches[0];
if (touch.position.x < Screen.width/2)
{
transform.position += new Vector3(0.1f,0,0);
}
else if (touch.position.x > Screen.width/2)
{
if (!grounded && rigidbody2D.velocity.y == 0) {
grounded = true;
}
if ((touch.position.x > Screen.width/2) && grounded == true) {
rigidbody2D.AddForce (transform.up * JumpSpeed);
grounded = false;
}
}
}
}
}
Thank You
Answer by monogon · Apr 27, 2014 at 11:21 PM
You only take into account one touch at a time
var touch = Input.touches[0];
You could use a foreach loop to loop through your multitouches. Something like this should work (untested):
foreach (Touch touch in Input.touches) {
// your if statements for movement and jump
if (touch.position.x < Screen.width/2)
{
transform.position += new Vector3(0.1f,0,0);
}
else if (touch.position.x > Screen.width/2)
{
if (!grounded && rigidbody2D.velocity.y == 0) {
grounded = true;
}
if ((touch.position.x > Screen.width/2) && grounded == true) {
rigidbody2D.AddForce (transform.up * JumpSpeed);
grounded = false;
}
}
Your answer
Follow this Question
Related Questions
MultiTouch for control scheme of my game (C#) 2 Answers
Two or more touchs at same time? 0 Answers
Problem with multi touch[solved] 0 Answers
Issues with using multitouch and event trigger on 2 joysticks 1 Answer
Multiple Cars not working 1 Answer