Search code examples
c#unity-game-enginetouchswipe

How do I call a method which is in another class, when swiping up, down, left or right? Unity2D


I have a Unity project that I started a couple of days ago. It is a simple 2D top down shooting game, which is aimed to be played on smartphone platforms.

I have a Shooting script, which basically has a method called Shoot1, which spawns in a bullet.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Shooting : MonoBehaviour
{
    public Transform firePoint;
    public GameObject BulletRedPrefab;
    public GameObject BulletGreenPrefab;
    public float bulletForce = 20f;
    // Update is called once per frame
    void Start()
    {

    }

    void Update()
    {

    }

    public IEnumerator Shoot1()
    {
        yield return new WaitForSeconds(0.00001f);
        GameObject bullet = Instantiate(BulletRedPrefab, firePoint.position, firePoint.rotation);
        Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
        rb.AddForce(firePoint.up * bulletForce, ForceMode2D.Impulse);
    }
}

I also have the Swipe script which determines the direction of the swipe, etc.

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;
    // Update is called once per frame
    private void Update()
    {
        tap = swipeUp = swipeDown = swipeLeft = swipeRight = false;
        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();
            }
        }

        if (Input.GetMouseButtonDown(0))
        {
            tap = true;
            isDraging = true;
            startTouch = Input.mousePosition;
        }
        else if (Input.GetMouseButtonUp(0))
        {
            isDraging = false;
            Reset();
        }

        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;
        }

        if (swipeDelta.magnitude > 125)
        {
            float x = swipeDelta.x;
            float y = swipeDelta.y;
            if (Mathf.Abs(x) > Mathf.Abs(y))
            {
                if (x < 0)
                    swipeLeft = true;
                else
                    swipeRight = true;
            }
            else
            {
                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; } }
}

And I have the GestureDetector script which is aimed to shoot a bullet whenever the user swipes left, right, up or down. When I tried to make the player object (which is called robot) move via swiping, it worked. But when I try calling a method from another class with swiping, it's not working.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GestrueDetector : MonoBehaviour
{
    public Shooting other;
    public Swipe swipeControls;
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    private void Update()
    {
        if (GameObject.Find("robot") != null)
        {
            if (swipeControls.SwipeLeft)
                desirePosition += Vector3.left;
            other.Shoot1();

            if (swipeControls.SwipeRight)
                desirePosition += Vector3.right;
            other.Shoot1();

            if (swipeControls.SwipeUp)
                desirePosition += Vector3.up;
            other.Shoot1();

            if (swipeControls.SwipeDown)
                desirePosition += Vector3.down;
            other.Shoot1();
        }
    }
}

I just started using unity this week, so I'm quite new to the software. Thank you very much!


Solution

  • For coroutines you need to call this methods like this:

    StartCoroutine(Shoot1());
    

    I a little bit changed your classes:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class Shooting : MonoBehaviour
    {
        public Transform firePoint;
        public GameObject BulletRedPrefab;
        public GameObject BulletGreenPrefab;
        public float bulletForce = 20f;
    
        void Start ()
        {
        }
    
        void Update()
        {
        }
    
        public void Shoot()
        {
           StartCoroutine(Shoot1());
        }
    
        private IEnumerator Shoot1()
        {
            yield return new WaitForSeconds(0.00001f);
            GameObject bullet = Instantiate(BulletRedPrefab, firePoint.position, firePoint.rotation);
            Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
            
            rb.AddForce(firePoint.up * bulletForce, ForceMode2D.Impulse);
        }
    }
    

    And for GestrueDetector:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class GestrueDetector : MonoBehaviour
    {
    public Shooting other;
    
    public Swipe swipeControls;
    
       
        // Start is called before the first frame update
        void Start()
        {
            
        }
    
        // Update is called once per frame
       private void Update()
        {
    
            if (GameObject.Find("robot") != null)
    {
        
            if (swipeControls.SwipeLeft)
                desirePosition += Vector3.left;
                  other.Shoot();    
    
            if (swipeControls.SwipeRight)
                desirePosition += Vector3.right;
                  other.Shoot();
    
                if (swipeControls.SwipeUp)
                desirePosition += Vector3.up;
                other.Shoot();
                     
    
                      
    
                if (swipeControls.SwipeDown)
                desirePosition += Vector3.down;
                other.Shoot();
                      
    
                      
    
    
    }
            }
        }
    

    Just read more about how coroutines are working in Unity - https://docs.unity3d.com/ScriptReference/Coroutine.html