【问题标题】:How do I call a method which is in another class, when swiping up, down, left or right? Unity2D向上、向下、向左或向右滑动时,如何调用另一个类中的方法? Unity2D
【发布时间】:2020-07-11 13:40:41
【问题描述】:

我有一个几天前开始的 Unity 项目。这是一款简单的 2D 自上而下的射击游戏,旨在在智能手机平台上玩。

我有一个 Shooting 脚本,它基本上有一个名为 Shoot1 的方法,它在子弹中生成。

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

我还有确定滑动方向等的 Swipe 脚本。

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

我有 GestureDetector 脚本,该脚本旨在在用户向左、向右、向上或向下滑动时发射子弹。当我试图通过滑动使玩家对象(称为机器人)移动时,它起作用了。但是当我尝试通过滑动调用另一个类的方法时,它不起作用。

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

我这周刚开始使用 unity,所以我对这个软件还很陌生。非常感谢!

【问题讨论】:

    标签: c# unity3d touch swipe


    【解决方案1】:

    对于协程,你需要像这样调用这个方法:

    StartCoroutine(Shoot1());
    

    我稍微改变了你的课程:

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

    对于 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();
                      
    
                      
    
    
    }
            }
        }
    

    阅读更多关于协程如何在 Unity 中工作的信息 - https://docs.unity3d.com/ScriptReference/Coroutine.html

    【讨论】:

    • 非常感谢!每当我向左、向右、向上或向下滑动时,都会调用该方法!唯一的问题是我构建了项目并将其导出到我的手机上进行测试(就像我通常做的那样),然后滑动,子弹会如我所愿产生,但不是在瞄准时产生。我希望能够用一根手指瞄准机器人,然后用另一根手指滑动来射击(用户需要在哪里滑动并不重要)。关于如何做到这一点的任何想法?谢谢!
    • 添加瞄准控制,也许只是做一个操纵杆瞄准,滑动保存TouchId。分开并定义触摸哪个用于瞄准和哪个用于射击
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-29
    • 1970-01-01
    • 1970-01-01
    • 2021-02-06
    • 1970-01-01
    • 1970-01-01
    • 2019-04-16
    相关资源
    最近更新 更多