如何在Unity中识别不同的接触?

问题描述

我创建了一个自上而下的2D游戏。游戏的主要目的是使敌人不断产卵,并且您必须向他们射击而不要让他们碰触您。该游戏旨在在智能手机设备上玩。

游戏应在智能手机上水平播放。我希望将屏幕分成一半,以便屏幕的左侧专用于操纵杆和瞄准(操纵杆用于瞄准),屏幕的右侧专用于滑动。现在,在我的游戏中,向上滑动可发射一种子弹,而向下滑动可发射另一种子弹。

当前,每当我按一下屏幕左侧的某个位置时,操纵杆就会出现并起作用,并且可以瞄准;每当我按一下屏幕右侧时,没有手指的时候,操纵杆没有出现,我也没有瞄准,这是完美的。我也可以滑动,如果我只用手指向下滑动(一根手指),则可以使用。目前,我什至可以在屏幕左侧滑动,但如果不是主要问题,我会尽快修复。

问题是,每当我将手指放在屏幕左侧并瞄准时,然后又放下另一根手指滑动时,角色就会开始从第二根手指(在右侧(据称只能滑动),并开始在第一根手指和第二根手指之间滑动。游戏杆未出现在屏幕右侧,这很好。

此外,无论何时我瞄准,我都不会滑动,因为什么也不会发生(除了目标开始出现故障)。

这是我的代码:

瞄准类:

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;

    
    
    // Update is called once per frame
    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,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);

     }else{

         
     }
     }
    
    
    
    void aimCharacter(Vector2 direction){
       
        Vector2 lookDir = pointB - pointA;
        float angle = Mathf.Atan2(lookDir.y,lookDir.x) * Mathf.Rad2Deg;
        rb.rotation = angle;
        
    }
    

   
    void shootBullet(){
       
    }
}

刷卡课程:

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;


        #region 
        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类别:

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)
              Debug.Log("LeftSwipe");
               

        if (swipeControls.SwipeRight)
               Debug.Log("RightSwipe");
            
            
            if (swipeControls.SwipeUp)
           
            other.ShootGreen();
                 

                  

            if (swipeControls.SwipeDown)
           
            other.Shoot();
                  

                  

        
}
        }



    }

关于如何解决这些错误的任何想法?谢谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)