@[TOC]unity学习
学习总结:Camera的Projection的两种模式对鼠标控制玩家移动的影响
在开发游戏的过程中,如果想让主角跟随鼠标移动一般是先获取鼠标位置,然后将鼠标位置转化成世界位置,之后再传递给主角:
// An highlighted block
Vector3 temp = camera1.ScreenToWorldPoint(Input.mousePosition);
GetComponent<NavMeshAgent>().destination = new Vector3(temp.x, 0.55f, temp.z);
这里用寻路做个例子,
但是我发现这种方法只在
Camera的Projection一共有两种模式:Camera是Orthographic的模式下时才能正常使用。
具体为什么在Perspective模式下就不行我也清楚。
但是在Perspective模式下有另一种方法也可以做到主角跟随的效果
RaycastHit hit;
private void Update()
{
if(Input.GetMouseButtonDown(0))
{
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray,out hit,100f))
{
//Debug.DrawRay(ray.origin,hit.point, Color.red);
//Debug.Log(hit.point.x);
GetComponent<NavMeshAgent>().destination = new Vector3(hit.point.x, 0.55f, hit.point.z);
}
}
}