Unity地图中点击角色移动功能

本篇讲大地图的人物图标瞬移。

瞬移:指的就是点击地图的一个地方,然后角色人物图标可以瞬间移动到这个坐标位置。

效果如图:当在地图中点击白色方块的地方,图标会跳到白色方块。对应的3D场景中的角色也会去对应的地方。

找到大地图的渲染的Rawimage。然后在它上面添加EventTrigger用来实现交互。接着添加新脚本Click map。

脚本的代码内容如下:

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;

public class ClickMap : MonoBehaviour, IPointerClickHandler
{
    public testmin minimap;
    private Vector2 tempVector;
    private Vector2 raypoint;
    public void OnPointerClick(PointerEventData eventData)
    {
        tempVector = new Vector2(eventData.pointerCurrentRaycast.screenPosition.x / Screen.width, eventData.pointerCurrentRaycast.screenPosition.y / Screen.height);

          Debug.Log("d点击位置"+tempVector);
      
        //通用分辨率
        raypoint = new Vector2((tempVector.x - (((Screen.width - minimap.maxmap.GetComponent<RectTransform>().sizeDelta.x) / 2) / Screen.width)) / (minimap.maxmap.GetComponent<RectTransform>().sizeDelta.x / Screen.width),
                 (tempVector.y - (((Screen.height - minimap.maxmap.GetComponent<RectTransform>().sizeDelta.y) / 2) / Screen.height)) / (minimap.maxmap.GetComponent<RectTransform>().sizeDelta.x / Screen.height));

        Debug.Log("RAYPOIT" + raypoint);

        Ray ray = minimap.minicamera.ViewportPointToRay(raypoint);
        RaycastHit hit;
        if (Physics.Raycast(ray, out hit, Mathf.Infinity))
        {
           
            {
                minimap.player.position = hit.point;
                Debug.Log("位置" + tempVector);
            }
           
        }
    }
}

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 18
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 18
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值