前段時間一直在琢磨vuforia for android ,考慮在這樣的情況下:世界座標中有多個物體,選中其中某個物體開始連線到另一個物體,沒想到vuforia for android 太坑了,花了很長時間也沒有實現。在vuforia for android中使用的是OPENGL.ES2.0實現畫各種圖形,vuforia會得到一個最終矩陣,把它傳入OPENGL,再傳入要畫的圖形的座標,就可以畫出ar的效果,但是得不到經過以上過程後圖形的最終座標,所以點擊的時候無法做出點擊時間。無奈之下換成vuforia for unity,之前的問題都很容易解決了。
1.選中物體:在世界座標下在多個物體中通過點擊選中某個物體
2.拖拽物體:在世界座標下選中某個物體並讓物體跟着手指移動
3.手指畫線:使用linerender,實現手指畫線
選中物體
void Update () {
Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
RaycastHit hit ;
if (Physics.Raycast(ray, out hit))
{
Debug.Log("產生碰撞,並且碰撞體的名字是:"+hit.collider.name);
}
else {
Debug.Log("沒有碰撞");
}
}
想要點擊的物體要添加碰撞體如box collider,如果是sphere,添加sphere collider等。。
拖拽物體:
using UnityEngine;
using System.Collections;
public class DragObject : MonoBehaviour
{
private Transform pickedObject = null;
private Vector3 lastPlanePoint;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
//創建一個平面
Plane targetPlane = new Plane(transform.up, transform.position);
foreach (Touch touch in Input.touches)
{
//獲取攝像頭近平面到屏幕觸摸點的射線
Ray ray = Camera.main.ScreenPointToRay(touch.position);
//獲取射線沿着plane的距離
float dist = 0.0f;
targetPlane.Raycast(ray, out dist);
//獲取沿着射線在距離dist位置的點
Vector3 planePoint = ray.GetPoint(dist);
//Debug.Log("Point=" + planePoint);
//按下手指觸碰屏幕
if (touch.phase == TouchPhase.Began)
{
RaycastHit hit = new RaycastHit();
// 判斷是否有碰撞到對象
if (Physics.Raycast(ray, out hit, 1000))
{
pickedObject = hit.transform;
lastPlanePoint = planePoint;
}
else
{
pickedObject = null;
}
//選中模型後拖拽
}
else if (touch.phase == TouchPhase.Moved)
{
if (pickedObject != null)
{
pickedObject.position += planePoint - lastPlanePoint;
lastPlanePoint = planePoint;
}
//釋放
}
else if (touch.phase == TouchPhase.Ended)
{
pickedObject = null;
}
}
}
}
給想要拖拽的物體綁定碰撞體,否則上述代碼沒用
**
手指畫線
**
創建一個空物體,添加lienrender組件,修改名字爲Line,上個有色材質,再創建一個Plane物體,給Plane添加碰撞體,上個透明材質,這樣點擊後產生的射線纔有可能產生碰撞
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
public class LineHelper3 : MonoBehaviour {
int lineSeg = 1;
RaycastHit rh ;
List<GameObject> pointPos;
LineRenderer lr;
GameObject pointer ;
bool start_point = true;
void Start()
{
pointer = new GameObject();
gameObject.SetActive(true);
lr = gameObject.GetComponent(typeof(LineRenderer)) as LineRenderer;
lr.SetWidth(1f, 1f);
}
// Update is called once per frame
void Update()
{
if (Input.GetTouch(0).phase == TouchPhase.Began)
{
Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
rh = new RaycastHit();
lr.SetVertexCount(1);
if (Physics.Raycast(ray, out rh))
{
pointPos.Add(DrawLine(rh, 0));
}
}
if (Input.GetTouch(0).phase == TouchPhase.Moved)
{
Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
rh = new RaycastHit();
lr.SetVertexCount(2);
if (Physics.Raycast(ray, out rh))
{
pointPos.Add(DrawLine(rh, 1));
}
}
if (Input.GetTouch(0).phase == TouchPhase.Ended)
{
start_point = true;
lr.SetVertexCount(0);
lr.SetPosition(0, new Vector3(0, 0, 0));
lr.SetPosition(1, new Vector3(0, 0, 0));
}
}
GameObject DrawLine(RaycastHit pointPos,int start_or_end)
{
//Display point
GameObject gb_pointer = GameObject.Instantiate(pointer) as GameObject;
gb_pointer.transform.position = pointPos.point + (transform.position - pointPos.point) * 0.01f;
gb_pointer.transform.rotation = Quaternion.LookRotation(pointPos.normal, Camera.main.transform.up);
Vector3 laserpos = new Vector3();
laserpos.x = 90.0f;
laserpos.y = gb_pointer.transform.position.y;
laserpos.z = gb_pointer.transform.position.z;
gb_pointer.transform.eulerAngles = laserpos;
lr.SetPosition(start_or_end, pointPos.point); //設置目標點的座標,使用的是world座標系
//lineSeg++;
return gb_pointer;
}
void DestroyLine()
{
int arrayLength = pointPos.Count;
if (arrayLength > 0)
{
GameObject.Destroy(pointPos[arrayLength - 1]);
pointPos.RemoveAt(arrayLength - 1);
//lr.SetVertexCount(--lineSeg);
}
}
}