Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question How can I shoot, by swiping up or down, whilst aiming?

Discussion in 'Input System' started by jonotris, Aug 16, 2020.

  1. jonotris

    jonotris

    Joined:
    Jul 2, 2020
    Posts:
    1
    I am quite new to Unity and C# (in fact, this is my first project). I am creating a simple 2D top down view shooter game. The game is aimed to be played on smartphones.

    I have a virtual, fixed joystick. I would like to be able to aim with one finger (on the joystick, on the left), and swipe up or down to fire (with another finger on the right). Right now, I can shoot perfectly. Where the character aims is based on the joystick. The problem is that the joystick can be controlled from anywhere on the left side of the screen if only one finger is on the screen (which makes the aiming weird). And, if I try to swipe on the right side of the screen (where the swiping should occur whilst playing), whilst aiming, the angle will start to be influenced by the second finger (the one that is swiping) as well. Also, I can't shoot while aiming (when I swipe, no effect happens, except the aiming angle gets glitchy).

    So what I would like to know is,
    1. How can I aim only when touching the joystick?
    2. How can I make it possible to not let other fingers influence the angle of aiming?
    3. How can I shoot by swiping, whilst aiming?


    This is the swiping script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Swipe : MonoBehaviour
    6. {
    7.  
    8.     private bool tap,swipeUp,swipeDown, swipeLeft, swipeRight;
    9.     private bool isDraging = false;
    10.     private Vector2 startTouch, swipeDelta;
    11.  
    12.  
    13.      // Update is called once per frame
    14.     private void Update()
    15.     {
    16.         tap = swipeUp = swipeDown = swipeLeft = swipeRight = false;
    17.  
    18.  
    19.         #region
    20.         if (Input.touches.Length > 0)
    21.         {
    22.  
    23.             if (Input.touches[0].phase == TouchPhase.Began){
    24.  
    25.                 isDraging = true;
    26.                 tap = true;
    27.                 startTouch = Input.touches[0].position;
    28.            
    29.  
    30.             }
    31.             else if (Input.touches[0].phase == TouchPhase.Ended || Input.touches[0].phase == TouchPhase.Canceled)
    32.         {
    33.  
    34.             isDraging = false;
    35.             Reset();
    36.  
    37.  
    38.         }
    39.  
    40.         }
    41.  
    42.  
    43.         #endregion
    44.  
    45.  
    46.  
    47.         #region Standalone Inputs
    48.         if (Input.GetMouseButtonDown(0)){
    49.  
    50.             tap = true;
    51.             isDraging = true;
    52.             startTouch = Input.mousePosition;
    53.  
    54.  
    55.  
    56.         }else if(Input.GetMouseButtonUp(0))
    57.         {
    58.  
    59.             isDraging = false;
    60.             Reset();
    61.  
    62.  
    63.         }
    64.         #endregion
    65.    
    66.         // Calculate the distance
    67.         swipeDelta = Vector2.zero;
    68.         if(isDraging)
    69.         {
    70.  
    71.             if(Input.touches.Length > 0)
    72.                 swipeDelta = Input.touches[0].position - startTouch;
    73.             else if (Input.GetMouseButton(0))
    74.                 swipeDelta = (Vector2)Input.mousePosition - startTouch;      
    75.         }
    76.    
    77.    
    78.         // Did we cross the deadzone?
    79.         if (swipeDelta.magnitude > 125)
    80.         {
    81.  
    82.             // Which direction?
    83.             float x = swipeDelta.x;
    84.             float y = swipeDelta.y;
    85.             if(Mathf.Abs(x) > Mathf.Abs(y))
    86.             {
    87.  
    88.                 // left or right
    89.                 if (x < 0)
    90.                     swipeLeft = true;
    91.  
    92.                 else
    93.                     swipeRight = true;
    94.  
    95.  
    96.  
    97.             }
    98.             else{
    99.  
    100.                 // up or down
    101.                 if (y < 0)
    102.                     swipeDown = true;
    103.  
    104.                 else
    105.                     swipeUp = true;
    106.            
    107.             }
    108.  
    109.  
    110.             Reset();
    111.  
    112.         }
    113.  
    114.  
    115.     }
    116.  
    117.  
    118.  
    119.  
    120.    
    121.    
    122.    
    123.      private void Reset()
    124.     {
    125.  
    126.         startTouch = swipeDelta = Vector2.zero;
    127.         isDraging = false;
    128.  
    129.  
    130.     }
    131.  
    132.  
    133.  
    134.  
    135.  
    136.  
    137.     public Vector2 SwipeDelta { get { return swipeDelta;}}
    138.     public bool SwipeUp { get { return swipeUp;}}
    139.     public bool SwipeDown { get { return swipeDown;}}
    140.     public bool SwipeLeft { get { return swipeLeft;}}
    141.     public bool SwipeRight{ get { return swipeRight;}}
    142. }
    143.  
    144.  
    145.  
    146.  
    147.  

    This is the GestureDetection script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GestrueDetector : MonoBehaviour
    6. {
    7. public Shooting other;
    8.  
    9. public Swipe swipeControls;
    10.  
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.        
    16.     }
    17.  
    18.     // Update is called once per frame
    19.    private void Update()
    20.     {
    21.  
    22.         if (GameObject.Find("robot") != null)
    23. {
    24.    
    25.         if (swipeControls.SwipeLeft)
    26.               Debug.Log("LeftSwipe");
    27.              
    28.  
    29.         if (swipeControls.SwipeRight)
    30.                Debug.Log("RightSwipe");
    31.            
    32.            
    33.             if (swipeControls.SwipeUp)
    34.          
    35.             other.Shoot();
    36.                
    37.  
    38.                  
    39.  
    40.             if (swipeControls.SwipeDown)
    41.          
    42.             other.Shoot();
    43.                  
    44.  
    45.                  
    46.  
    47.        
    48. }
    49.         }
    50.  
    51.  
    52.  
    53.     }
    54.  

    This is the script to aim:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class joystickShoot : MonoBehaviour {
    6.     public Shooting other;
    7.     public Rigidbody2D rb;
    8.     public Swipe swipeControls;
    9.     private bool touchStart = false;
    10.  
    11.     private Vector2 pointA;
    12.     private Vector2 pointB;
    13.     public Transform player;
    14.     public float speed = 15.0f;
    15.     public GameObject bulletPrefab;
    16.  
    17.     public Transform circle;
    18.     public Transform outerCircle;
    19.  
    20.     private Vector2 startingPoint;
    21.     private int leftTouch = 99;
    22.  
    23.    
    24.     // Update is called once per frame
    25.     void Update () {
    26.         int i = 0;
    27.         while(i < Input.touchCount){
    28.             Touch t = Input.GetTouch(i);
    29.             Vector2 touchPos = getTouchPosition(t.position); // * -1 for perspective cameras
    30.             if(t.phase == TouchPhase.Began){
    31.                 if(t.position.x > Screen.width / 2){
    32.                    
    33.                    
    34.                  
    35.                 }else{
    36.                     leftTouch = t.fingerId;
    37.                      pointA = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z));
    38.                     startingPoint = touchPos;
    39.                     touchStart = true;
    40.                 }
    41.             }else if(t.phase == TouchPhase.Moved && leftTouch == t.fingerId){
    42.                touchStart = true;
    43.                 Vector2 offset = touchPos - startingPoint;
    44.                 Vector2 direction = Vector2.ClampMagnitude(offset, 1.0f);
    45.                
    46.                  pointB = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z));
    47.              
    48.                 aimCharacter(direction);
    49.  
    50.                 circle.transform.position = new Vector2(outerCircle.transform.position.x + direction.x, outerCircle.transform.position.y + direction.y);
    51.  
    52.             }else if(t.phase == TouchPhase.Ended && leftTouch == t.fingerId){
    53.                 leftTouch = 99;
    54.                 circle.transform.position = new Vector2(outerCircle.transform.position.x, outerCircle.transform.position.y);
    55.             }else{
    56.  
    57.                 touchStart = false;
    58.             }
    59.             ++i;
    60.         }
    61.  
    62.     }
    63.     Vector2 getTouchPosition(Vector2 touchPosition){
    64.         return GetComponent<Camera>().ScreenToWorldPoint(new Vector3(touchPosition.x, touchPosition.y, transform.position.z));
    65.     }
    66.  
    67.     void aimCharacter(Vector2 direction){
    68.      
    69.         Vector2 lookDir = pointB - pointA;
    70.         float angle = Mathf.Atan2(lookDir.y, lookDir.x) * Mathf.Rad2Deg;
    71.         rb.rotation = angle;
    72.        
    73.     }
    74.    
    75.  
    76.      private void FixedUpdate() {
    77.           if(touchStart){
    78.             Vector2 offset = pointB - pointA;
    79.  
    80.  
    81.            
    82.            
    83.            
    84.             Vector2 direction = Vector2.ClampMagnitude(offset, 1.0f);
    85.             aimCharacter(direction * 1);
    86.  
    87.      }
    88.      }
    89.     void shootBullet(){
    90.      
    91.     }
    92. }

    Any ideas regarding what I can do? Thanks!