Unity3D点击某处物体跟着移动

Unity3D点击某处物体跟着移动_第1张图片

配置好环境之后,将一个cube放在imagetarget上面。并且设置一个plane,属性为shadows only

Unity3D点击某处物体跟着移动_第2张图片

再ARCamera的子物体Camera上挂上脚本goThere.cs(注意一定要挂在Camera上,挂在ARcamera上就无法运行)

Unity3D点击某处物体跟着移动_第3张图片

然后点击运行,在点击的时候这个物体就能移动到点击的位置了。

using UnityEngine;
using System.Collections;

public class goThere : MonoBehaviour
{

	public Camera cam;
	private Vector3 hitPoint;
	public GameObject meinv;
	public float speed = 30;
	// Use this for initialization
	void Start()
	{
		cam = this.GetComponent();
	}

	// Update is called once per frame
	void Update()
	{
		if (Input.GetMouseButtonDown(0))
		{
			Ray ray = cam.ScreenPointToRay(Input.mousePosition);
			RaycastHit hit;   
			if (Physics.Raycast(ray, out hit))
			{
				if (hit.collider.gameObject.name == "Plane")
				{
					hitPoint = new Vector3(hit.point.x, meinv.transform.position.y, hit.point.z);
					Debug.Log(hitPoint);
				}
			}
		}
		meinv.transform.position = Vector3.MoveTowards(meinv.transform.position, hitPoint, Time.deltaTime * speed);
	}

}

你可能感兴趣的:(AR,UNITY3D)