注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Faye Euler's blog

技術交流歡迎留言

 
 
 

日志

 
 

【转载】高通拖动模型 Drag an AR Object on Screen with Your Finger  

2015-03-14 22:25:30|  分类: Unity3D |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

using UnityEngine;
using System.Collections;
 
public class MyDragBehaviour : MonoBehaviour
{
    private float maxPickingDistance = 2000;// increase if needed, depending on your scene size
     
    private Transform pickedObject = null;
  
    // Use this for initialization
    void Start ()
    {
    }
  
    // Update is called once per frame
    void Update ()
    {
        foreach (Touch touch in Input.touches)
        {
            Debug.Log("Touching at: " + touch.position);
             
            //Gets the ray at position where the screen is touched
            Ray ray = Camera.main.ScreenPointToRay(touch.position);
       
            if (touch.phase == TouchPhase.Began)
            {
                Debug.Log("Touch phase began at: " + touch.position);
    
               RaycastHit hit = new RaycastHit();
               if (Physics.Raycast(ray, out hit, maxPickingDistance))
               {
                   pickedObject = hit.transform;                   
               }
               else
               {
                   pickedObject = null;
               }
            }
            else if (touch.phase == TouchPhase.Moved)
            {
                Debug.Log("Touch phase Moved");
    
                if (pickedObject != null)
                {
                    Vector2 screenDelta = touch.deltaPosition;
      
                    float halfScreenWidth = 0.5f * Screen.width;
                    float halfScreenHeight = 0.5f * Screen.height;
      
                    float dx = screenDelta.x / halfScreenWidth;
                    float dy = screenDelta.y / halfScreenHeight;
      
                    Vector3 objectToCamera =
                        pickedObject.transform.position - Camera.main.transform.position;
                    float distance = objectToCamera.magnitude;
      
                    float fovRad = Camera.main.fieldOfView * Mathf.Deg2Rad;
                    float motionScale = distance * Mathf.Tan(fovRad/2);
      
                    Vector3 translationInCameraRef =
                        new Vector3(motionScale * dx, motionScale * dy, 0);
      
                    Vector3 translationInWorldRef =
                        Camera.main.transform.TransformDirection(translationInCameraRef);
       
                    pickedObject.position += translationInWorldRef;
                }
            }
            else if (touch.phase == TouchPhase.Ended)
            {
                Debug.Log("Touch phase Ended");
    
                pickedObject = null;
            }
        }
    }
}


  评论这张
 
阅读(713)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018