20.8 C
New York

unity – How Do I Separate Dragging My Finger On Joystick And Swiping?


I’m at present making a 2D top-down view shooter sport for Android, the place the sport is performed horizontally.

On the left aspect of the display screen, you possibly can contact anyplace to spawn in a joystick and intention by dragging your finger, and that works fully wonderful.

You can too swipe and it shoots. Swiping up shoots one kind of bullet, while swiping down shoots one other.

The issue is when doing each on the identical time. If I begin aiming, after which swipe, the swipe will not work, and the angle for aiming begins being learn additionally from the contact place of the finger swiping, and this glitches the aiming.

Swipe Script:

utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing UnityEngine;

public class Swipe : MonoBehaviour
{    
    non-public bool faucet,swipeUp,swipeDown, swipeLeft, swipeRight;
    non-public bool isDraging = false;
    non-public Vector2 startTouch, swipeDelta;

    non-public void Replace()
    {
        faucet = swipeUp = swipeDown = swipeLeft = swipeRight = false;
    
        #area Contact Inputs
        if (Enter.touches.Size > 0)
        {
            if (Enter.touches[0].part == TouchPhase.Started){
                isDraging = true;
                faucet = true;
                startTouch = Enter.touches[0].place;
            }
            else if (Enter.touches[0].part == TouchPhase.Ended || Enter.touches[0].part == TouchPhase.Canceled)
            {

                isDraging = false;
                Reset();
            }
        }
        #endregion
      
        #area Standalone Inputs
        if (Enter.GetMouseButtonDown(0)){
            faucet = true;
            isDraging = true;
            startTouch = Enter.mousePosition;  
        }else if(Enter.GetMouseButtonUp(0))
        {    
            isDraging = false;
            Reset();
        }
        #endregion
    
        // Calculate the gap
        swipeDelta = Vector2.zero;
        if(isDraging)
        {    
            if(Enter.touches.Size > 0)
                swipeDelta = Enter.touches[0].place - startTouch;
            else if (Enter.GetMouseButton(0))
                swipeDelta = (Vector2)Enter.mousePosition - startTouch;        
        }
    
    
        // Did we cross the deadzone?
        if (swipeDelta.magnitude > 125)
        {    
            // Which path?
            float x = swipeDelta.x;
            float y = swipeDelta.y;
            if(Mathf.Abs(x) > Mathf.Abs(y))
            {    
                // left or proper
                if (x < 0)
                    swipeLeft = true;

                else
                    swipeRight = true;
            }
            else{    
                // up or down
                if (y < 0)
                    swipeDown = true;

                else
                    swipeUp = true;                
            }    

            Reset();    
        }  
    }

    non-public 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:

utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing UnityEngine;

public class GestureDetector : MonoBehaviour
{
    public Taking pictures different;

    public Swipe swipeControls;

   
    non-public void Replace()
    {    
        if (GameObject.Discover("robotic") != null) {
    
            if (swipeControls.SwipeLeft)
                 Debug.Log("LeftSwipe");
        
            if (swipeControls.SwipeRight)
                 Debug.Log("RightSwipe");
            
            
            if (swipeControls.SwipeUp)
                 different.ShootGreen();

            if (swipeControls.SwipeDown)
                 different.Shoot();
        }
    }
}
   

Joystick Script:

utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing UnityEngine;

public class joystickShoot : MonoBehaviour {
    public Taking pictures different;
    public Rigidbody2D rb;
    
    non-public bool touchStart = false;

    non-public Vector2 pointA;
    non-public Vector2 pointB;
    public Remodel participant;
    public float pace = 15.0f;
    public GameObject bulletPrefab;

    public Remodel circle;
    public Remodel outerCircle;

    non-public Vector2 startingPoint;
    non-public int leftTouch = 99;


    void Replace () {
        int i = 0;
        whereas(i < Enter.touchCount){
            Contact t = Enter.GetTouch(i);
            Vector2 touchPos = getTouchPosition(t.place); // * -1 for perspective cameras
            if(t.part == TouchPhase.Started){
                if(t.place.x > Display.width / 2) {
                } else {    
                    touchStart = true;
                    leftTouch = t.fingerId;
                      pointA = Digital camera.predominant.ScreenToWorldPoint(new Vector3(Enter.mousePosition.x, Enter.mousePosition.y, Digital camera.predominant.remodel.place.z));
                    circle.remodel.place = pointA * 1;
                    outerCircle.remodel.place = pointA * 1;
                    circle.GetComponent<SpriteRenderer>().enabled = true;
                    outerCircle.GetComponent<SpriteRenderer>().enabled = true;    
                    startingPoint = touchPos;
                    
                }
            }else if(t.part == TouchPhase.Moved && leftTouch == t.fingerId){
               touchStart = true;
                Vector2 offset = touchPos - startingPoint;
                Vector2 path = Vector2.ClampMagnitude(offset, 1.0f);
                
                 pointB = Digital camera.predominant.ScreenToWorldPoint(new Vector3(Enter.mousePosition.x, Enter.mousePosition.y, Digital camera.predominant.remodel.place.z));
               
                aimCharacter(path);
                circle.remodel.place = new Vector2(pointA.x + path.x, pointA.y + path.y) * 1;
                circle.remodel.place = new Vector2(outerCircle.remodel.place.x + path.x, outerCircle.remodel.place.y + path.y);

            }else if(t.part == TouchPhase.Ended && leftTouch == t.fingerId){
                
                circle.GetComponent<SpriteRenderer>().enabled = false;
            outerCircle.GetComponent<SpriteRenderer>().enabled = false;
                leftTouch = 99;
                circle.remodel.place = new Vector2(outerCircle.remodel.place.x, outerCircle.remodel.place.y);
            }else{

                touchStart = false;
                
            }
            ++i;
        }    
    }

    Vector2 getTouchPosition(Vector2 touchPosition){
        return GetComponent<Digital camera>().ScreenToWorldPoint(new Vector3(touchPosition.x, touchPosition.y, remodel.place.z));
    }

    non-public void FixedUpdate() {
          if (touchStart) {
            Vector2 offset = pointB - pointA;                
            Vector2 path = Vector2.ClampMagnitude(offset, 1.0f);
            aimCharacter(path * 1);    
          }
    }
    
    
    void aimCharacter(Vector2 path){           
        Vector2 lookDir = pointB - pointA;
        float angle = Mathf.Atan2(lookDir.y, lookDir.x) * Mathf.Rad2Deg;
        rb.rotation = angle;            
    }    
}

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles