Need some Help with Swipes!
So I got a little problem ;( I was playing around with touches and swipes, and i made a little project. It contains 2 cubes and 1 Ground.
The Project is made for phones and just for phones. My only goal to achieve is that the topper cube "Jumps" when i swipe up on the left half of the phone screen, and if i swipe down it will apply a negativ force. Same goes with the bottom cube, except that the bottom cube has negative gravity on its Rigidbody 2d component, and if you want it to jump down, you have to swipe down on the right side of the screen. Now, I made 4 scripts for this and it kind of works, i can swipe with one Finger and the cubes react how they are supposed to, and i can do it with 2 fingers to, if i time it right and put both fingers up at the exact same time, but if i touch with one finger and before lifting my finger begin eith the second touch, it wont work ;( It will cancel or dont even apply one of the forces. I want it to react on every swipe!
So these are my scripts:
1) First touch on left side for topper cube
public class Player1Jump1 : MonoBehaviour {
public float jumpForce, maxTimeInterval, touchPosX;
float touchTimeStart, touchTimeFinish, timeInterval;
Vector2 startPos, endPos, direction;
void Update()
{
#region 1st Touch top cube
//If you touch the screen
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
touchTimeStart = Time.time;
startPos = Input.GetTouch(0).position;
touchPosX = Input.GetTouch(0).position.x;
}
//If you lift your finger from the screen
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended)
{
touchTimeFinish = Time.time;
timeInterval = touchTimeFinish - touchTimeStart;
endPos = Input.GetTouch(0).position;
direction = startPos - endPos;
if (timeInterval < maxTimeInterval)
{
if (touchPosX < 640)
{
GetComponent<Rigidbody2D>().AddForce(new Vector2(0, -direction.y * jumpForce));
}
}
}
#endregion
}//end of update void
}//end of all
2) Second touch on left side for topper cube
public class Player1Jump2 : MonoBehaviour {
public float jumpForce, maxTimeInterval, touchPosX;
float touchTimeStart, touchTimeFinish, timeInterval;
Vector2 startPos, endPos, direction;
void Update()
{
#region 2nd Touch top cube
//If you touch the screen
if (Input.touchCount > 1 && Input.GetTouch(1).phase == TouchPhase.Began)
{
touchTimeStart = Time.time;
startPos = Input.GetTouch(1).position;
touchPosX = Input.GetTouch(1).position.x;
}
//If you lift your finger from the screen
if (Input.touchCount > 1 && Input.GetTouch(1).phase == TouchPhase.Ended)
{
touchTimeFinish = Time.time;
timeInterval = touchTimeFinish - touchTimeStart;
endPos = Input.GetTouch(1).position;
direction = startPos - endPos;
if (timeInterval < maxTimeInterval)
{
if (touchPosX < 640)
{
GetComponent<Rigidbody2D>().AddForce(new Vector2(0, -direction.y * jumpForce));
}
}
}
#endregion
}//end of update void
}//end of all
3) First touch on right side for bottom cube
public class Player2Jump1 : MonoBehaviour {
public float jumpForce, maxTimeInterval, touchPosX;
float touchTimeStart, touchTimeFinish, timeInterval;
Vector2 startPos, endPos, direction;
void Update()
{
#region 1st Touch bottom cube
//If you touch the screen
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
touchTimeStart = Time.time;
startPos = Input.GetTouch(0).position;
touchPosX = Input.GetTouch(0).position.x;
}
//If you lift your finger from the screen
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended)
{
touchTimeFinish = Time.time;
timeInterval = touchTimeFinish - touchTimeStart;
endPos = Input.GetTouch(0).position;
direction = startPos - endPos;
if (timeInterval < maxTimeInterval)
{
if (touchPosX > 640)
{
GetComponent<Rigidbody2D>().AddForce(new Vector2(0, -direction.y * jumpForce));
}
}
}
#endregion
}//end of update void
}//end of all
4) Second touch on right side for bottom cube
public class Player2Jump2 : MonoBehaviour {
public float jumpForce, maxTimeInterval, touchPosX;
float touchTimeStart, touchTimeFinish, timeInterval;
Vector2 startPos, endPos, direction;
void Update()
{
#region 2nd Touch bottom cube
//If you touch the screen
if (Input.touchCount > 1 && Input.GetTouch(1).phase == TouchPhase.Began)
{
touchTimeStart = Time.time;
startPos = Input.GetTouch(1).position;
touchPosX = Input.GetTouch(1).position.x;
}
//If you lift your finger from the screen
if (Input.touchCount > 1 && Input.GetTouch(1).phase == TouchPhase.Ended)
{
touchTimeFinish = Time.time;
timeInterval = touchTimeFinish - touchTimeStart;
endPos = Input.GetTouch(1).position;
direction = startPos - endPos;
if (timeInterval < maxTimeInterval)
{
if (touchPosX > 640)
{
GetComponent<Rigidbody2D>().AddForce(new Vector2(0, -direction.y * jumpForce));
}
}
}
#endregion
}//end of update void
}//end of all
Hopefully you can see threw it and help me out ;)
yours sincerly
NeoN