[unity3d]navmesh 自动寻路 鼠标点击的坐标获取 鼠标点击的世界坐标

问题起因:下载里一套unity3d rpg的源码,里面有一座桥,我想移植到自己的游戏中去,但是发现第一人称控制器直接会穿过这座桥,根本无法在其表面行走。我在原来的工程里复制了一个桥墩,依然无法产生任何交互。经过询问之后,猜测可能是由于主角脚本里写了自动寻路navmesh,却没有把复制的桥墩的路径加进去,所以无法行走。但是我看了一下源码,也没有看的很懂。先把这个navmesh搞定,也算是个基本的知识吧。navmesh其实就是navigate mesh导航网格的意思。

学习navmesh 参考:http://liweizhaolili.blog.163.com/blog/static/16230744201271161310135/

自动寻路牵扯到一个问题,鼠标点击位置的世界点坐标:

using UnityEngine;
using System.Collections;

public class targetmove : MonoBehaviour {
	float x;
	float y;
	private RaycastHit hit = new RaycastHit();
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		if (Input.GetMouseButton(0)){
			x = Input.GetAxis("Mouse X");
			y = Input.GetAxis("Mouse Y");
			// MonoBehaviour.print(x);
			// print(y);
			// this.transform.position = Vector3(x, y,0);
			// transform.Rotate(new Vector3(x,y,0));
			// print(y);
			// Transform.Translate(float x, float y, float z, Transform relativeTo);
			
			// Debug.Log(Input.mousePosition);
			// Ray ray = Camera.ScreenPointToRay(Input.mousePosition);
			// RaycastHit hit;
			// if (Physics.Raycast(ray,out hit,100)){
				// Point p = hit.point;
				// print(hit.point);
			// }
			// transform.Translate(52, 15, 51);
			transform.position = new Vector3(52,15,51);
			// transform.Translate(2,0,1);
			Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        	Physics.Raycast(ray,out hit,100);
        	if(null != hit.transform)
        	{
            	print(hit.point);
            	// iTween.MoveTo(player,hit.point,10.0f);
        	}
        	// transform.Translate(new Vector3(hit.point.x,hit.point.y,hit.point.z));
		}
	}
}

但是桥面的问题依然没有解决,不知道为什么呢。

是不是因为与tag有关?


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值