Как связать долгое нажатие, чтобы переместить скрипт - PullRequest
0 голосов
/ 12 апреля 2019

Я нашел этот скрипт: https://unity3d.college/2018/01/30/unity3d-ugui-hold-click-buttons/

Я использую Vuforia, кстати.

Мне было интересно, как связать его с моим скриптом движения.

   using UnityEngine;

    public class MyDragBehaviour : MonoBehaviour
    {
    void Update()
    {
        if (Input.touchCount == 1 && Input.GetTouch(0).phase == 
    TouchPhase.Moved)
        {
            // create ray from the camera and passing through the touch 
    position:
            Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
            // create a logical plane at this object's position
            // and perpendicular to world Y:
            Plane plane = new Plane(Vector3.up, transform.position);
            float distance = 0; // this will return the distance from the camera
            if (plane.Raycast(ray, out distance))
            { // if plane hit...
                Vector3 pos = ray.GetPoint(distance); // get the point
                transform.position = pos;                                      
    // pos has the position in the plane you've touched
            }
        }
    }
    }

Текущий скрипт движения мгновенно переместит мой объект туда, где произошло касание на экране.Я бы хотел, чтобы вы долго нажимали на объект, прежде чем перемещать его, чтобы объект не подпрыгивал на экране.

EDIT

using UnityEngine;
using UnityEngine.EventSystems;

public class MyDragBehaviour : MonoBehaviour
{


float pointerDownTimer = 0;
const float requiredHoldTime = 0.5f; //has to hold for 0.5 seconds

void Update()
    {
        if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved)
        {
            pointerDownTimer += Time.deltaTime;

            if (pointerDownTimer >= requiredHoldTime)
            {
                pointerDownTimer = 0;

                if (!EventSystem.current.IsPointerOverGameObject())

                    if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved)
                    {
                        // create ray from the camera and passing through the touch position:
                        Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
                        // create a logical plane at this object's position
                        // and perpendicular to world Y:
                        Plane plane = new Plane(Vector3.up, transform.position);
                        float distance = 0; // this will return the distance from the camera
                        if (plane.Raycast(ray, out distance))
                        { // if plane hit...
                            Vector3 pos = ray.GetPoint(distance); // get the point
                            transform.position = pos;                                      // pos has the position in the plane you've touched
                        } //whatever happens when you click

                    }
        }
        else
        {
            pointerDownTimer = 0;
        }
    }



    }
}

1 Ответ

1 голос
/ 12 апреля 2019

Вам просто нужно реализовать простой таймер, который увеличивается при нажатии и сбрасывается при отпускании:

float pointerDownTimer = 0;
const float requiredHoldTime = 0.5f //has to hold for 0.5 seconds

void Update(){
    if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved)
    {
       pointerDownTimer += Time.deltaTime; 

       if (pointerDownTimer >= requiredHoldTime){

           ...... //whatever happens when you click

       }
    } else{
       pointerDownTimer = 0;
    }
}
...