如何通过在屏幕上滑动手指来放置对象? [英] How to place objects by swipe your finger across the screen?

查看:107
本文介绍了如何通过在屏幕上滑动手指来放置对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用教程,我们可以放置对象用手指轻拍表面上的.

Using this tutorial we can place objects on the surface by finger tap.

我们如何更改脚本以在屏幕上滑动手指的同时放置对象,以便放置对象就像应该在其上放置对象的绘画"区域一样?

How can we change the script to place objects while swiping a finger across the screen, so that placing objects would be like "painting" area where they should be placed?

以下是用于放置本教程的脚本:

Here is the script for placing from the tutorial:

using UnityEngine;
using System.Collections;

public class KittyUIController : MonoBehaviour
{
    public GameObject m_kitten;
    private TangoPointCloud m_pointCloud;

    void Start()
    {
        m_pointCloud = FindObjectOfType<TangoPointCloud>();
    }

    void Update ()
    {
        if (Input.touchCount == 1)
        {
            // Trigger place kitten function when single touch ended.
            Touch t = Input.GetTouch(0);
            if (t.phase == TouchPhase.Ended)
            {
                PlaceKitten(t.position);
            }
        }
    }

    void PlaceKitten(Vector2 touchPosition)
    {
        // Find the plane.
        Camera cam = Camera.main;
        Vector3 planeCenter;
        Plane plane;
        if (!m_pointCloud.FindPlane(cam, touchPosition, out planeCenter, out plane))
        {
            Debug.Log("cannot find plane.");
            return;
        }

        // Place kitten on the surface, and make it always face the camera.
        if (Vector3.Angle(plane.normal, Vector3.up) < 30.0f)
        {
            Vector3 up = plane.normal;
            Vector3 right = Vector3.Cross(plane.normal, cam.transform.forward).normalized;
            Vector3 forward = Vector3.Cross(right, plane.normal).normalized;
            Instantiate(m_kitten, planeCenter, Quaternion.LookRotation(forward, up));
        }
        else
        {
            Debug.Log("surface is too steep for kitten to stand on.");
        }
    }
}

推荐答案

除了在触摸阶段结束时生成小猫,您还可以在触摸移动时生成它们:

Instead of spawning a kitten when the touchphase ends, you can spawn them whenever the touch moves: TouchPhase.Moved. Be warned - this will spawn A LOT of kittens while you drag since this is being checked every frame while in the Update() method. Consider adding a time delay, or only spawning after the finger moves a certain distance.

    void Update ()
    {
        if (Input.touchCount == 1)
        {
            // Trigger place kitten function when single touch moves.
            Touch t = Input.GetTouch(0);
            if (t.phase == TouchPhase.Moved)
            {
                PlaceKitten(t.position);
            }
        }
    }

可以像这样实现距离检查:

A distance check could be implemented like so:

float Vector3 oldpos;

void Update ()
{
    if (Input.touchCount == 1)
    {
        Touch t = Input.GetTouch(0);
        if (t.phase == TouchPhase.Began)
        {
            oldpos = t.position; //get initial touch position
        }
        if (t.phase == TouchPhase.Moved)
        {
            // check if the distance between stored position and current touch position is greater than "2"
            if (Mathf.Abs(Vector3.Distance(oldpos, t.position)) > 2f)
            {
                PlaceKitten(t.position);
                oldpos = t.position; // store position for next distance check
            }
        }
    }
}

这篇关于如何通过在屏幕上滑动手指来放置对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆