Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.XR.iOS;
- public class CatControl : MonoBehaviour {
- public Transform hitTransform;
- Vector3 GetLookVector () {
- Vector3 lookVector =
- Camera.main.transform.position - hitTransform.transform.position;
- lookVector.y = 0.0f; // Y軸の差分は無視する
- lookVector.Normalize ();
- return lookVector;
- }
- bool HitTestWithResultType (ARPoint point, ARHitTestResultType resultTypes) {
- List<ARHitTestResult> hitResults =
- UnityARSessionNativeInterface.GetARSessionNativeInterface ().
- HitTest (point, resultTypes);
- if (hitResults.Count > 0) {
- foreach (var hitResult in hitResults) {
- Debug.Log ("Got hit!");
- hitTransform.position =
- UnityARMatrixOps.GetPosition (hitResult.worldTransform);
- hitTransform.transform.rotation =
- Quaternion.LookRotation (GetLookVector());
- return true;
- }
- }
- return false;
- }
- // Use this for initialization
- void Start () {
- }
- // Update is called once per frame
- void Update () {
- if (Input.touchCount > 0 && hitTransform != null) {
- var touch = Input.GetTouch (0);
- if (touch.phase == TouchPhase.Began || touch.phase == TouchPhase.Moved) {
- var screenPosition =
- Camera.main.ScreenToViewportPoint (touch.position);
- ARPoint point = new ARPoint {
- x = screenPosition.x,
- y = screenPosition.y
- };
- // prioritize reults types
- ARHitTestResultType[] resultTypes = {
- ARHitTestResultType.ARHitTestResultTypeExistingPlaneUsingExtent,
- // if you want to use infinite planes use this:
- //ARHitTestResultType.ARHitTestResultTypeExistingPlane,
- ARHitTestResultType.ARHitTestResultTypeHorizontalPlane,
- ARHitTestResultType.ARHitTestResultTypeFeaturePoint
- };
- foreach (ARHitTestResultType resultType in resultTypes) {
- if (HitTestWithResultType (point, resultType)) {
- return;
- }
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment