Unity实现点击小地图上某位置,让player移动到那里

发表于2018-07-22
评论0 4.2k浏览
很多手游中都能做到点击小地图上的某一点,游戏角色就会移动到该点位置,此功能在开发是如何实现的呢,下面就和大家介绍下实现步骤。

1、创建场景和player,烘焙一下场景:

2、为player添加NavMeshAgent组件。

3、添加小地图:


4、玫瑰色立方体块的Layer设置为target

5、把下面脚本挂到主相机上:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class RayTest : MonoBehaviour {
    private Ray ray;
    private RaycastHit hit;//射线碰到的碰撞信息
    public GameObject navPlayer;//寻路的人
    private NavMeshAgent agent;
    public LayerMask myLayer;
    public Camera mapCamera;
    private void Start()
    {
        agent = navPlayer.GetComponent<NavMeshAgent>();
    }
    private void Update ()
    {
        //射线起始位置
        ray = mapCamera.ScreenPointToRay(Input.mousePosition);
        if(Physics.Raycast(ray,out hit, 100, myLayer))
        {
            Debug.DrawLine(ray.origin, hit.transform.position, Color.red);
            if (Input.GetMouseButtonDown(0))
                agent.SetDestination(hit.transform.position);
        }
	}
}

在小地图上点击玫瑰色方块,player走到相应位置。
来自:https://blog.csdn.net/wwanrong/article/details/76207085

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引