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";
    }
相关推荐
企客宝CRM2 分钟前
2026年中小企业CRM选型指南:企客宝CRM处于什么位置?
android·算法·企业微信·rxjava·crm
橙淮5 分钟前
二叉树核心概念与Java实现详解
数据结构·算法
米罗篮31 分钟前
DSU并查集 & 拓展欧几里得-逆元
c++·经验分享·笔记·算法·青少年编程
橙淮31 分钟前
双指针法:高效算法解题的利器
算法
初心未改HD41 分钟前
深度学习之MLP与反向传播算法详解
人工智能·深度学习·算法
刀法如飞43 分钟前
【Go 字符串查找的 20 种实现方式,用不同思路解决问题】
人工智能·算法·go
技术小黑3 小时前
CNN算法实战系列03 | DenseNet121算法实战与解析
pytorch·深度学习·算法·cnn
wearegogog1233 小时前
三电平SVPWM逆变器仿真指南
单片机·算法
笨笨饿3 小时前
74_SysTick滴答定时器中断
c语言·开发语言·人工智能·单片机·嵌入式硬件·算法·学习方法