UI显示任务目的地标记的方法

首先我们都知道用Camera.main.WorldToScreenPoint(),这是目的地在屏幕内的情况。

  1. 如果在屏幕外但还在玩家前方,对坐标的xy使用Mathf.Clamp()限制到屏幕里就行。
  2. 如果目的地在玩家后方,我选择根据是偏左还是偏右粗暴的放到屏幕最左或最右;

注意WorldToScreenPoint()的结果受屏幕分辨率影响,分辨率1920*1080和16:9 Aspect的效果不一样。所以加上pos.x * canvasRT.width / Screen.width消除影响。

简单粗暴的方法

注意z<0也就是在玩家身后时,x>Screen.width / 2反而意味着偏左。

cs 复制代码
void GetMarkerPos()
{
    Vector3 pos=Camera.main.WorldToScreenPoint(chasingMission.transform.position);
    if (pos.z > 0)
    {
        pos.x = Mathf.Clamp(pos.x,0, Screen.width);
        pos.y = Mathf.Clamp(pos.y,0, Screen.height);
    }
    else {
        pos.x = pos.x < Screen.width / 2 ? Screen.width : 0;
        pos.y=Screen.height / 2;
    }
    pos = new Vector2(pos.x * canvasRT.width / Screen.width,
        pos.y * canvasRT.height/Screen.height);
    string distanceHint = ((int)Vector3.Distance(player.transform.position,
    chasingMission.transform.position)).ToString() + "m";
    onUpdateMarker?.Invoke(pos, distanceHint);
}

一个之前的方法,很麻烦

cs 复制代码
Rect scale;
void Start(){
    Rect screen = (GameSceneManager.Instance.canvas.transform as RectTransform).rect;
    //画布的rect宽高固定为1920*1080,即使分辨率只是自由16:9
    scale.width = screen.width / Screen.width;
    scale.height = screen.height / Screen.height;
}
void CalculateMissionMarkerPosition(){
            targetDir=targetMission.transform.position-player.transform.position;
            if(Vector3.Angle(player.transform.forward,targetDir)<Camera.main.fieldOfView){
                Vector2 pos=Camera.main.WorldToScreenPoint(targetMission.transform.position);
                pos = new Vector2(pos.x / Screen.width * 1920, pos.y / Screen.height * 1080);
                missionMarker.anchoredPosition=pos;
            }
            else{
                if(cinemachineBrain.ActiveVirtualCamera==null){
                    return;//在第一帧可能ActiveVirtualCamera是空
                }
                Vector3 destInPlayer=cinemachineBrain.ActiveVirtualCamera.VirtualCameraGameObject.
                transform.InverseTransformPoint(targetMission.transform.position);
                Vector2 pos;
                if(Mathf.Abs(destInPlayer.y/destInPlayer.x)>(float)Screen.height/Screen.width){
                    pos.x=Mathf.Abs(destInPlayer.x/destInPlayer.y*Screen.height/2);
                    if(destInPlayer.x<0){
                        pos.x=-pos.x;
                    }
                    if(destInPlayer.y>0){
                        pos.y=Screen.height/2;
                    }
                    else{
                        pos.y=-Screen.height/2;
                    }
                }
                else{
                    pos.y=Mathf.Abs(destInPlayer.y/destInPlayer.x*Screen.width/2);
                    if(destInPlayer.y<0){
                        pos.y=-pos.y;
                    }
                    if(destInPlayer.x>0){
                        pos.x=Screen.width/2;
                    }
                    else{
                        pos.x=-Screen.width/2;
                    }
                }
                missionMarker.anchoredPosition=pos+new Vector2(Screen.width,Screen.height)/2;
            }
            textDistance.text = ((int)Vector3.Distance(MyInput.Instance.player.transform.position,
            targetMission.transform.position)).ToString()+"m";
    }
相关推荐
政企项目老覃9 小时前
大模型幻觉治理与自动评测:金融风控场景的落地实践
人工智能·算法·机器学习
插件开发9 小时前
Illustrator 插件开发:链接文件存在检查的原理与实战
ui·illustrator
淡海水9 小时前
08-03-不可变-ImmutableDictionary-TKey-TValue-与ImmutableHashSet-T-持久化哈希树
数据结构·算法·c#·哈希算法·dictionary·immutable
hansang_IR9 小时前
【题解】[APIO2023] 赛博乐园 / cyberland
c++·算法·图论
xy34539 小时前
axure9.0 如何打造一个计时器(简单版)
前端·ui·html·axure·原型·产品设计
洛阳纸贵9 小时前
MATLAB-matlab基础知识
学习·算法·matlab
落羽的落羽10 小时前
【AI】快速理解AI应用的相关名词概念
linux·c++·人工智能·python·计算机网络·算法
Nil20810 小时前
leetcode 17电话号码的字母组合
算法·leetcode·职场和发展
203号居民11 小时前
LeetCode hot 100 — 25. K 个一组翻转链表
算法·leetcode·链表