我有一个有效的滑动检测器脚本,但是我不知道如何将其连接到我的角色控制器。任何帮助,将不胜感激

问题描述 投票:-1回答:1

我的游戏是一个无尽的奔跑者,角色只需要沿y轴移动。我想发生的是,玩家根据滑动而上下移动,我认为可以做到这一点,方法是说如果玩家触发了onSwipeUp或Down,那么他们将朝该方向移动,但是我不能使它工作。请帮助我。

这是我尝试在其中执行滑动控件之前的播放器控制器脚本:

public class Player : MonoBehaviour
{

private Vector2 targetPos;

public float yIncrement;
public float maxHeight;
public float minHeight; 

private void Update()
    {

    transform.position = Vector2.MoveTowards(transform.position, targetPos, speed * Time.deltaTime);

    if (Input.GetKeyDown(KeyCode.W) && transform.position.y < maxHeight)
    {
        Instantiate(effect, transform.position, Quaternion.identity);
        targetPos = new Vector2(transform.position.x, transform.position.y + yIncrement);
    }
    else if (Input.GetKeyDown(KeyCode.S) && transform.position.y > minHeight)
    {
        Instantiate(effect, transform.position, Quaternion.identity);
        targetPos = new Vector2(transform.position.x, transform.position.y - yIncrement);
    }
}

这是滑动检测脚本:

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

public class SwipeTest : MonoBehaviour
{
    private Vector2 fingerDown;
    private Vector2 fingerUp;
    public bool detectSwipeOnlyAfterRelease = false;
public float SWIPE_THRESHOLD = 20f;

// Update is called once per frame
void Update()
{

    foreach (Touch touch in Input.touches)
    {
        if (touch.phase == TouchPhase.Began)
        {
            fingerUp = touch.position;
            fingerDown = touch.position;
        }

        //Detects Swipe while finger is still moving
        if (touch.phase == TouchPhase.Moved)
        {
            if (!detectSwipeOnlyAfterRelease)
            {
                fingerDown = touch.position;
                checkSwipe();
            }
        }

        //Detects swipe after finger is released
        if (touch.phase == TouchPhase.Ended)
        {
            fingerDown = touch.position;
            checkSwipe();
        }
    }
}

void checkSwipe()
{
    //Check if Vertical swipe
    if (verticalMove() > SWIPE_THRESHOLD && verticalMove() > horizontalValMove())
    {
        //Debug.Log("Vertical");
        if (fingerDown.y - fingerUp.y > 0)//up swipe
        {
            OnSwipeUp();
        }
        else if (fingerDown.y - fingerUp.y < 0)//Down swipe
        {
            OnSwipeDown();
        }
        fingerUp = fingerDown;
    }

    //Check if Horizontal swipe
    else if (horizontalValMove() > SWIPE_THRESHOLD && horizontalValMove() > verticalMove())
    {
        //Debug.Log("Horizontal");
        if (fingerDown.x - fingerUp.x > 0)//Right swipe
        {
            OnSwipeRight();
        }
        else if (fingerDown.x - fingerUp.x < 0)//Left swipe
        {
            OnSwipeLeft();
        }
        fingerUp = fingerDown;
    }

    //No Movement at-all
    else
    {
        //Debug.Log("No Swipe!");
    }
}

float verticalMove()
{
    return Mathf.Abs(fingerDown.y - fingerUp.y);
}

float horizontalValMove()
{
    return Mathf.Abs(fingerDown.x - fingerUp.x);
}

//////////////////////////////////CALLBACK FUNCTIONS/////////////////////////////


 public void OnSwipeUp()
    {
        Debug.Log("Swipe UP");
    }

   public void OnSwipeDown()
    {
        Debug.Log("Swipe Down");
    }

    void OnSwipeLeft()
    {
        Debug.Log("Swipe Left");
    }

    void OnSwipeRight()
    {
        Debug.Log("Swipe Right");
    }
}
c# ios unity3d controls
1个回答
0
投票

您可以为每个滑动方向设置一个公共布尔值。在更新的第一行中,将这些布尔值设置为false,在onswipe函数中,将各个布尔值设置为true。然后从播放器脚本中引用swipetest脚本,并检查所需的布尔布尔值是否设置为true。

代码看起来像这样:

玩家:

public class Player : MonoBehaviour
{

private Vector2 targetPos;

public float yIncrement;
public float maxHeight;
public float minHeight; 

public SwipeTest swipetest;

private void OnEnable()
{
  swipetest = (SwipeTest)FindObjectOfType(typeof(SwipeTest));
}

private void Update()
    {

    transform.position = Vector2.MoveTowards(transform.position, targetPos, speed * Time.deltaTime);

    if (swipetest.swipeUp && transform.position.y < maxHeight)
    {
        Instantiate(effect, transform.position, Quaternion.identity);
        targetPos = new Vector2(transform.position.x, transform.position.y + yIncrement);
    }
    else if (swipetest.swipeDown && transform.position.y > minHeight)
    {
        Instantiate(effect, transform.position, Quaternion.identity);
        targetPos = new Vector2(transform.position.x, transform.position.y - yIncrement);
    }
}

swipetest

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

public class SwipeTest : MonoBehaviour
{
    private Vector2 fingerDown;
    private Vector2 fingerUp;
    public bool detectSwipeOnlyAfterRelease = false;
public float SWIPE_THRESHOLD = 20f;

public bool swipeUp = false;
public bool swipeDown = false;
public bool swipeLeft = false;
public bool swipeRight = false;


// Update is called once per frame
void Update()
{

  swipeUp = false;
  swipeDown = false;
  swipeLeft = false;
  swipeRight = false;

    foreach (Touch touch in Input.touches)
    {
        if (touch.phase == TouchPhase.Began)
        {
            fingerUp = touch.position;
            fingerDown = touch.position;
        }

        //Detects Swipe while finger is still moving
        if (touch.phase == TouchPhase.Moved)
        {
            if (!detectSwipeOnlyAfterRelease)
            {
                fingerDown = touch.position;
                checkSwipe();
            }
        }

        //Detects swipe after finger is released
        if (touch.phase == TouchPhase.Ended)
        {
            fingerDown = touch.position;
            checkSwipe();
        }
    }
}

void checkSwipe()
{
    //Check if Vertical swipe
    if (verticalMove() > SWIPE_THRESHOLD && verticalMove() > horizontalValMove())
    {
        //Debug.Log("Vertical");
        if (fingerDown.y - fingerUp.y > 0)//up swipe
        {
            OnSwipeUp();
        }
        else if (fingerDown.y - fingerUp.y < 0)//Down swipe
        {
            OnSwipeDown();
        }
        fingerUp = fingerDown;
    }

    //Check if Horizontal swipe
    else if (horizontalValMove() > SWIPE_THRESHOLD && horizontalValMove() > verticalMove())
    {
        //Debug.Log("Horizontal");
        if (fingerDown.x - fingerUp.x > 0)//Right swipe
        {
            OnSwipeRight();
        }
        else if (fingerDown.x - fingerUp.x < 0)//Left swipe
        {
            OnSwipeLeft();
        }
        fingerUp = fingerDown;
    }

    //No Movement at-all
    else
    {
        //Debug.Log("No Swipe!");
    }
}

float verticalMove()
{
    return Mathf.Abs(fingerDown.y - fingerUp.y);
}

float horizontalValMove()
{
    return Mathf.Abs(fingerDown.x - fingerUp.x);
}

//////////////////////////////////CALLBACK FUNCTIONS/////////////////////////////


 public void OnSwipeUp()
    {
      swipeUp = true;
    }

   public void OnSwipeDown()
    {
      swipeDown = true;
    }

    void OnSwipeLeft()
    {
      swipeLeft = true;
    }

    void OnSwipeRight()
    {
      swipeRight = true;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.