I am currently making a 2D top-down view shooter game for Android, where the game is played horizontally.
On the left side of the screen, you can touch anywhere to spawn in a joystick and aim by dragging your finger, and that works completely fine.
You can also swipe and it shoots. Swiping up shoots one type of bullet, whilst swiping down shoots another.
The problem is when doing both at the same time. If I start aiming, and then swipe, the swipe won't work, and the angle for aiming starts being read also from the touch position of the finger swiping, and this glitches the aiming.
Swipe Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Swipe : MonoBehaviour
{
private bool tap,swipeUp,swipeDown, swipeLeft, swipeRight;
private bool isDraging = false;
private Vector2 startTouch, swipeDelta;
private void Update()
{
tap = swipeUp = swipeDown = swipeLeft = swipeRight = false;
#region Touch Inputs
if (Input.touches.Length > 0)
{
if (Input.touches[0].phase == TouchPhase.Began){
isDraging = true;
tap = true;
startTouch = Input.touches[0].position;
}
else if (Input.touches[0].phase == TouchPhase.Ended || Input.touches[0].phase == TouchPhase.Canceled)
{
isDraging = false;
Reset();
}
}
#endregion
#region Standalone Inputs
if (Input.GetMouseButtonDown(0)){
tap = true;
isDraging = true;
startTouch = Input.mousePosition;
}else if(Input.GetMouseButtonUp(0))
{
isDraging = false;
Reset();
}
#endregion
// Calculate the distance
swipeDelta = Vector2.zero;
if(isDraging)
{
if(Input.touches.Length > 0)
swipeDelta = Input.touches[0].position - startTouch;
else if (Input.GetMouseButton(0))
swipeDelta = (Vector2)Input.mousePosition - startTouch;
}
// Did we cross the deadzone?
if (swipeDelta.magnitude > 125)
{
// Which direction?
float x = swipeDelta.x;
float y = swipeDelta.y;
if(Mathf.Abs(x) > Mathf.Abs(y))
{
// left or right
if (x < 0)
swipeLeft = true;
else
swipeRight = true;
}
else{
// up or down
if (y < 0)
swipeDown = true;
else
swipeUp = true;
}
Reset();
}
}
private void Reset()
{
startTouch = swipeDelta = Vector2.zero;
isDraging = false;
}
public Vector2 SwipeDelta { get { return swipeDelta;}}
public bool SwipeUp { get { return swipeUp;}}
public bool SwipeDown { get { return swipeDown;}}
public bool SwipeLeft { get { return swipeLeft;}}
public bool SwipeRight{ get { return swipeRight;}}
}
GestureDetection Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GestureDetector : MonoBehaviour
{
public Shooting other;
public Swipe swipeControls;
private void Update()
{
if (GameObject.Find("robot") != null) {
if (swipeControls.SwipeLeft)
Debug.Log("LeftSwipe");
if (swipeControls.SwipeRight)
Debug.Log("RightSwipe");
if (swipeControls.SwipeUp)
other.ShootGreen();
if (swipeControls.SwipeDown)
other.Shoot();
}
}
}
Joystick Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class joystickShoot : MonoBehaviour {
public Shooting other;
public Rigidbody2D rb;
private bool touchStart = false;
private Vector2 pointA;
private Vector2 pointB;
public Transform player;
public float speed = 15.0f;
public GameObject bulletPrefab;
public Transform circle;
public Transform outerCircle;
private Vector2 startingPoint;
private int leftTouch = 99;
void Update () {
int i = 0;
while(i < Input.touchCount){
Touch t = Input.GetTouch(i);
Vector2 touchPos = getTouchPosition(t.position); // * -1 for perspective cameras
if(t.phase == TouchPhase.Began){
if(t.position.x > Screen.width / 2) {
} else {
touchStart = true;
leftTouch = t.fingerId;
pointA = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z));
circle.transform.position = pointA * 1;
outerCircle.transform.position = pointA * 1;
circle.GetComponent<SpriteRenderer>().enabled = true;
outerCircle.GetComponent<SpriteRenderer>().enabled = true;
startingPoint = touchPos;
}
}else if(t.phase == TouchPhase.Moved && leftTouch == t.fingerId){
touchStart = true;
Vector2 offset = touchPos - startingPoint;
Vector2 direction = Vector2.ClampMagnitude(offset, 1.0f);
pointB = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z));
aimCharacter(direction);
circle.transform.position = new Vector2(pointA.x + direction.x, pointA.y + direction.y) * 1;
circle.transform.position = new Vector2(outerCircle.transform.position.x + direction.x, outerCircle.transform.position.y + direction.y);
}else if(t.phase == TouchPhase.Ended && leftTouch == t.fingerId){
circle.GetComponent<SpriteRenderer>().enabled = false;
outerCircle.GetComponent<SpriteRenderer>().enabled = false;
leftTouch = 99;
circle.transform.position = new Vector2(outerCircle.transform.position.x, outerCircle.transform.position.y);
}else{
touchStart = false;
}
++i;
}
}
Vector2 getTouchPosition(Vector2 touchPosition){
return GetComponent<Camera>().ScreenToWorldPoint(new Vector3(touchPosition.x, touchPosition.y, transform.position.z));
}
private void FixedUpdate() {
if (touchStart) {
Vector2 offset = pointB - pointA;
Vector2 direction = Vector2.ClampMagnitude(offset, 1.0f);
aimCharacter(direction * 1);
}
}
void aimCharacter(Vector2 direction){
Vector2 lookDir = pointB - pointA;
float angle = Mathf.Atan2(lookDir.y, lookDir.x) * Mathf.Rad2Deg;
rb.rotation = angle;
}
}