I seem to be unable to detect a double touch, it interferes with the slide gesture implemented already.
using UnityEngine;
using System.Collections;
public class SlideToMove_02 : MonoBehaviour
{
private Touch initialTouch = new Touch();
private float distance = 0;
private bool hasSwiped = false;
public float speed = 5;
public static Rigidbody2D coby;
Quaternion origRotation;
Animator anim;
void Awake()
{
Debug.Log ("Awake");
}
void Start()
{
anim = GetComponent<Animator>();
coby = GetComponent<Rigidbody2D>();
origRotation = transform.rotation;
}
void Update()
{
#region Tap to rotate
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint((Input.GetTouch(0).position)), Vector2.zero);
if (hit.collider != null)
{
Debug.Log("Touched it");
transform.Rotate(0, 180, 0);
}
}
#endregion*/
}
void LateUpdate()
{
CheckBounds();
}
void FixedUpdate()
{
#region Computer Input
float h = Input.GetAxis ("Horizontal");
float v = Input.GetAxis ("Vertical");
Vector2 move = new Vector2 (h, v);
coby.AddForce (move * speed / 2);
#endregion
#region Touch Input
foreach (Touch t in Input.touches) {
if (t.phase == TouchPhase.Began) {
initialTouch = t;
} else if (t.phase == TouchPhase.Moved && !hasSwiped) {
float deltaX = initialTouch.position.x - t.position.x;
float deltaY = initialTouch.position.y - t.position.y;
distance = Mathf.Sqrt ((deltaX * deltaX) + (deltaY * deltaY));
bool swipedSideways = Mathf.Abs (deltaX) > Mathf.Abs (deltaY);
if (distance > 10f) {
if (swipedSideways && deltaX > 0) { //swiped left
transform.rotation = origRotation;
coby.AddForce (new Vector3 (-100f, 0f, 0f) * speed * Time.deltaTime);
transform.Rotate (0, 180, 0);
//anim.SetTrigger("SwipeLeft");
} else if (swipedSideways && deltaX <= 0) { //swiped right
coby.AddForce (new Vector3 (100f, 0f, 0f) * speed * Time.deltaTime);
transform.rotation = origRotation;
//anim.SetTrigger("SwipeRight");
} else if (!swipedSideways && deltaY > 0) { //swiped down
transform.rotation = origRotation;
coby.AddForce (new Vector3 (0f, -100f, 0f) * speed * Time.deltaTime);
transform.Rotate (90, 0, 0);
//anim.SetTrigger("SwipeDown");
} else if (!swipedSideways && deltaY <= 0) { //swiped up
transform.rotation = origRotation;
coby.AddForce (new Vector3 (0f, 100f, 0f) * speed * Time.deltaTime);
transform.Rotate (270, 0, 0);
//anim.SetTrigger("SwipeUp");
}
hasSwiped = true;
}
} else if (t.phase == TouchPhase.Ended) {
initialTouch = new Touch ();
hasSwiped = false;
}
}
#endregion
}
//Screen Wrapping done
void CheckBounds()
{
if (this.transform.position.x <= -9.11f)
{
this.transform.position = new Vector3(9.11f, this.transform.position.y, 0);
}
if (this.transform.position.x > 9.11f)
{
this.transform.position = new Vector3(-9.11f, this.transform.position.y, 0);
}
if (this.transform.position.y <= -5.6f)
{
this.transform.position = new Vector3(this.transform.position.x, 5.6f, 0);
}
if (this.transform.position.y > 5.6f)
{
this.transform.position = new Vector3(this.transform.position.x, -5.6f, 0);
}
}
}