Unity 抬头显示 屏幕外跟踪标记

2024-01-29 22:40

本文主要是介绍Unity 抬头显示 屏幕外跟踪标记,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

简介

游戏中经常会需要实现屏幕外标记指示的功能,这样能方便玩家快速定位目标方位,尤其在多人网络游戏中出现(守望先锋,彩虹六号,Apex英雄等等):

屏外显示实例

具体实现

现在使用Unity实现,其实要复杂可以很复杂,这里肯定用简单直接点的,核心功能是Unity的Unity - Scripting API: Camera.WorldToScreenPoint (unity3d.com)https://docs.unity3d.com/ScriptReference/Camera.WorldToScreenPoint.html

这个API可以直接将特定物体在屏幕上的相对坐标输出出来,真是非常Nice!

在Unity Canvas中加入一个Image便可,箭头的话放入子集,可能需要一个空物体做parent来当作旋转轴心,然后在Canvas组件中加入如下脚本,然后在Unity中指定每个变量。部分解析在代码注释中,不保证是最佳解决方法,但是确实能用。

using UnityEngine;
/* 
* Copyright (c) [2023] [Lizhneghe.Chen https://github.com/Lizhenghe-Chen]
*/
//https://blog.csdn.net/DUYIJIU/article/details/98366918
//https://indienova.com/indie-game-development/unity-off-screen-objective-marker/
public class TargetToScreen : MonoBehaviour
{[Header("Assign below variables in the inspector:")][SerializeField] Transform TargetTransform;[SerializeField] Vector3 TargetPositionOffset = new Vector3(0, 1, 0);[SerializeField] float screenBoundOffset = 0.1f;[SerializeField] RectTransform TargetImage, TargetImageArrow;[SerializeField] TMPro.TextMeshProUGUI TargetText;[Header("Below is for Debug use:")][SerializeField] Vector3 screenPosition, screenBound;[SerializeField] Vector2 Arrowdirection;private void LateUpdate(){TargetToScreenPosition();}/// <summary>/// This function is to convert the world position of the target to the screen position, and then update the position of the target image and the arrow image./// </summary>public void TargetToScreenPosition(){screenPosition = Camera.main.WorldToScreenPoint(TargetTransform.position + TargetPositionOffset);// simple way to get the screen position of the target(screenBound.x, screenBound.y) = (Screen.width, Screen.height);//get the screen sizeif (screenPosition.z < 0)//if the target is behind the camera, flip the screen position{screenPosition = -screenPosition;if (screenPosition.y > screenBound.y / 2)//this is to avoid small probablity that the target is behind the camera and the screen position is flipped, but the y position is still in the screen{screenPosition.y = screenBound.y;}else screenPosition.y = 0;}//clamp the screen position to the screen boundTargetImage.transform.position = new Vector2(Mathf.Clamp(screenPosition.x, screenBound.y * screenBoundOffset, screenBound.x - screenBound.y * screenBoundOffset),Mathf.Clamp(screenPosition.y, screenBound.y * screenBoundOffset, screenBound.y - screenBound.y * screenBoundOffset));//optional, rotate the arrow to point to the target:Arrowdirection = TargetImageArrow.transform.position - screenPosition;//get the direction of the arrow by subtracting the screen position of the target from the screen position of the arrow// Debug.Log(Arrowdirection.magnitude);if (Mathf.Abs(Arrowdirection.x + Arrowdirection.y) < 0.1){//disable the arrow when the target is too close to the center of the screenTargetImageArrow.gameObject.SetActive(false);//TargetImageArrow.transform.up = Vector2.zero;}else{TargetImageArrow.gameObject.SetActive(true);TargetImageArrow.transform.up = Arrowdirection;}//optional, update the distance textTargetText.text = Vector3.Distance(TargetTransform.position, Camera.main.transform.position).ToString("F1") + "m";}
}

实际效果展示,略糊,蓝绿色图标

这篇关于Unity 抬头显示 屏幕外跟踪标记的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/658396

相关文章

使用Python实现一键隐藏屏幕并锁定输入

《使用Python实现一键隐藏屏幕并锁定输入》本文主要介绍了使用Python编写一个一键隐藏屏幕并锁定输入的黑科技程序,能够在指定热键触发后立即遮挡屏幕,并禁止一切键盘鼠标输入,这样就再也不用担心自己... 目录1. 概述2. 功能亮点3.代码实现4.使用方法5. 展示效果6. 代码优化与拓展7. 总结1.

Python+PyQt5实现多屏幕协同播放功能

《Python+PyQt5实现多屏幕协同播放功能》在现代会议展示、数字广告、展览展示等场景中,多屏幕协同播放已成为刚需,下面我们就来看看如何利用Python和PyQt5开发一套功能强大的跨屏播控系统吧... 目录一、项目概述:突破传统播放限制二、核心技术解析2.1 多屏管理机制2.2 播放引擎设计2.3 专

Python实战之屏幕录制功能的实现

《Python实战之屏幕录制功能的实现》屏幕录制,即屏幕捕获,是指将计算机屏幕上的活动记录下来,生成视频文件,本文主要为大家介绍了如何使用Python实现这一功能,希望对大家有所帮助... 目录屏幕录制原理图像捕获音频捕获编码压缩输出保存完整的屏幕录制工具高级功能实时预览增加水印多平台支持屏幕录制原理屏幕

一文详解SQL Server如何跟踪自动统计信息更新

《一文详解SQLServer如何跟踪自动统计信息更新》SQLServer数据库中,我们都清楚统计信息对于优化器来说非常重要,所以本文就来和大家简单聊一聊SQLServer如何跟踪自动统计信息更新吧... SQL Server数据库中,我们都清楚统计信息对于优化器来说非常重要。一般情况下,我们会开启"自动更新

Linux虚拟机不显示IP地址的解决方法(亲测有效)

《Linux虚拟机不显示IP地址的解决方法(亲测有效)》本文主要介绍了通过VMware新装的Linux系统没有IP地址的解决方法,主要步骤包括:关闭虚拟机、打开VM虚拟网络编辑器、还原VMnet8或修... 目录前言步骤0.问题情况1.关闭虚拟机2.China编程打开VM虚拟网络编辑器3.1 方法一:点击还原VM

CSS模拟 html 的 title 属性(鼠标悬浮显示提示文字效果)

《CSS模拟html的title属性(鼠标悬浮显示提示文字效果)》:本文主要介绍了如何使用CSS模拟HTML的title属性,通过鼠标悬浮显示提示文字效果,通过设置`.tipBox`和`.tipBox.tipContent`的样式,实现了提示内容的隐藏和显示,详细内容请阅读本文,希望能对你有所帮助... 效

Python利用自带模块实现屏幕像素高效操作

《Python利用自带模块实现屏幕像素高效操作》这篇文章主要为大家详细介绍了Python如何利用自带模块实现屏幕像素高效操作,文中的示例代码讲解详,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1、获取屏幕放缩比例2、获取屏幕指定坐标处像素颜色3、一个简单的使用案例4、总结1、获取屏幕放缩比例from

如何设置vim永久显示行号

《如何设置vim永久显示行号》在Linux环境下,vim默认不显示行号,这在程序编译出错时定位错误语句非常不便,通过修改vim配置文件vimrc,可以在每次打开vim时永久显示行号... 目录设置vim永久显示行号1.临时显示行号2.永www.chinasem.cn久显示行号总结设置vim永久显示行号在li

电脑显示hdmi无信号怎么办? 电脑显示器无信号的终极解决指南

《电脑显示hdmi无信号怎么办?电脑显示器无信号的终极解决指南》HDMI无信号的问题却让人头疼不已,遇到这种情况该怎么办?针对这种情况,我们可以采取一系列步骤来逐一排查并解决问题,以下是详细的方法... 无论你是试图为笔记本电脑设置多个显示器还是使用外部显示器,都可能会弹出“无HDMI信号”错误。此消息可能

第10章 中断和动态时钟显示

第10章 中断和动态时钟显示 从本章开始,按照书籍的划分,第10章开始就进入保护模式(Protected Mode)部分了,感觉从这里开始难度突然就增加了。 书中介绍了为什么有中断(Interrupt)的设计,中断的几种方式:外部硬件中断、内部中断和软中断。通过中断做了一个会走的时钟和屏幕上输入字符的程序。 我自己理解中断的一些作用: 为了更好的利用处理器的性能。协同快速和慢速设备一起工作