Guest User

Untitled

a guest
Feb 19th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.XR.iOS;
  5.  
  6. public class CatControl : MonoBehaviour {
  7. public Transform hitTransform;
  8.  
  9. Vector3 GetLookVector () {
  10. Vector3 lookVector =
  11. Camera.main.transform.position - hitTransform.transform.position;
  12. lookVector.y = 0.0f; // Y軸の差分は無視する
  13. lookVector.Normalize ();
  14. return lookVector;
  15. }
  16.  
  17. bool HitTestWithResultType (ARPoint point, ARHitTestResultType resultTypes) {
  18. List<ARHitTestResult> hitResults =
  19. UnityARSessionNativeInterface.GetARSessionNativeInterface ().
  20. HitTest (point, resultTypes);
  21. if (hitResults.Count > 0) {
  22. foreach (var hitResult in hitResults) {
  23. Debug.Log ("Got hit!");
  24. hitTransform.position =
  25. UnityARMatrixOps.GetPosition (hitResult.worldTransform);
  26. hitTransform.transform.rotation =
  27. Quaternion.LookRotation (GetLookVector());
  28. return true;
  29. }
  30. }
  31. return false;
  32. }
  33.  
  34. // Use this for initialization
  35. void Start () {
  36.  
  37. }
  38.  
  39. // Update is called once per frame
  40. void Update () {
  41. if (Input.touchCount > 0 && hitTransform != null) {
  42. var touch = Input.GetTouch (0);
  43. if (touch.phase == TouchPhase.Began || touch.phase == TouchPhase.Moved) {
  44. var screenPosition =
  45. Camera.main.ScreenToViewportPoint (touch.position);
  46. ARPoint point = new ARPoint {
  47. x = screenPosition.x,
  48. y = screenPosition.y
  49. };
  50.  
  51. // prioritize reults types
  52. ARHitTestResultType[] resultTypes = {
  53. ARHitTestResultType.ARHitTestResultTypeExistingPlaneUsingExtent,
  54. // if you want to use infinite planes use this:
  55. //ARHitTestResultType.ARHitTestResultTypeExistingPlane,
  56. ARHitTestResultType.ARHitTestResultTypeHorizontalPlane,
  57. ARHitTestResultType.ARHitTestResultTypeFeaturePoint
  58. };
  59.  
  60. foreach (ARHitTestResultType resultType in resultTypes) {
  61. if (HitTestWithResultType (point, resultType)) {
  62. return;
  63. }
  64. }
  65. }
  66. }
  67. }
  68. }
Add Comment
Please, Sign In to add comment