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

来源:互联网 发布:程序员有前途吗 编辑:程序博客网 时间:2024/05/01 13:32

问题起因:下载里一套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 initializationvoid Start () {}// Update is called once per framevoid 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有关?


0 0