如何通过在屏幕上滑动手指来放置对象?
How to place objects by swipe your finger across the screen?
使用this教程,我们可以通过手指点击将对象放置在表面上。
我们如何更改脚本以在屏幕上滑动手指时放置对象,以便放置对象就像 "painting" 应放置对象的区域?
这是教程中用于放置的脚本:
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.");
}
}
}
您可以在触摸移动时生成小猫,而不是在触摸阶段结束时生成小猫:TouchPhase.Moved。请注意 - 这会在您拖动时产生很多小猫,因为在 Update()
方法中每帧都会检查它。考虑增加一个时间延迟,或者只在手指移动一定距离后才生成。
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);
}
}
}
距离检查可以这样实现:
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
}
}
}
}
使用this教程,我们可以通过手指点击将对象放置在表面上。
我们如何更改脚本以在屏幕上滑动手指时放置对象,以便放置对象就像 "painting" 应放置对象的区域?
这是教程中用于放置的脚本:
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.");
}
}
}
您可以在触摸移动时生成小猫,而不是在触摸阶段结束时生成小猫:TouchPhase.Moved。请注意 - 这会在您拖动时产生很多小猫,因为在 Update()
方法中每帧都会检查它。考虑增加一个时间延迟,或者只在手指移动一定距离后才生成。
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);
}
}
}
距离检查可以这样实现:
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
}
}
}
}