繁体   English   中英

我有一个有效的滑动检测器脚本,但我不知道如何将它连接到我的角色控制器

[英]I have a working swipe detector script, but I don't know how to connect it to my character controller

我的游戏是一个无尽的跑步者,角色只需要沿着 y 轴移动。 我想要发生的是玩家根据滑动向上或向下移动,我认为我可以通过说明如果玩家触发 onSwipeUp 或向下然后他们会朝那个方向移动来做到这一点,但我不能让它工作。

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

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

您可以为每个滑动方向设置一个公共布尔值。 在更新的第一行将这些布尔值设置为 false,在 onswipe 函数中将相应的布尔值设置为 true。 然后从播放器脚本中引用 swipetest 脚本并检查所需的 swipe 布尔值是否设置为 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);
    }
}

刷卡测试

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM