I have a working swipe detector script, but i don't know how to connect it to my character controller. Any help would be appreciated

Dylan Zukerberg :

My game is an endless runner and the character only needs to move along the y-axis. What I want to happen is that the player moves up or down depending on the swipe, and I thought that I could do it by stating that if the player triggered the onSwipeUp or down then they would move in that direction, but I couldn't get it working. Please help me.

This is the player controller script before I tried implementing swipe controls into it:

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

And this is the swipe detecting script:

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

You could set a public boolean for each swipe direction. In the first lines of update set these booleans to false, in the onswipe functions set the respective booleans to true. Then reference the swipetest script from the player script and check if the desired swipe boolean is set to true.

The code would look something like this:

player:

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=220142&siteId=1