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屏幕抓取和录制的相关资料,需要的朋友可以参考... 目录一、常用 python 屏幕抓取库二、pyautogui 截屏示例三、mss 高性能截图四、Pill

C#实现SHP文件读取与地图显示的完整教程

《C#实现SHP文件读取与地图显示的完整教程》在地理信息系统(GIS)开发中,SHP文件是一种常见的矢量数据格式,本文将详细介绍如何使用C#读取SHP文件并实现地图显示功能,包括坐标转换、图形渲染、平... 目录概述功能特点核心代码解析1. 文件读取与初始化2. 坐标转换3. 图形绘制4. 地图交互功能缩放

Unity新手入门学习殿堂级知识详细讲解(图文)

《Unity新手入门学习殿堂级知识详细讲解(图文)》Unity是一款跨平台游戏引擎,支持2D/3D及VR/AR开发,核心功能模块包括图形、音频、物理等,通过可视化编辑器与脚本扩展实现开发,项目结构含A... 目录入门概述什么是 UnityUnity引擎基础认知编辑器核心操作Unity 编辑器项目模式分类工程

C#和Unity中的中介者模式使用方式

《C#和Unity中的中介者模式使用方式》中介者模式通过中介者封装对象交互,降低耦合度,集中控制逻辑,适用于复杂系统组件交互场景,C#中可用事件、委托或MediatR实现,提升可维护性与灵活性... 目录C#中的中介者模式详解一、中介者模式的基本概念1. 定义2. 组成要素3. 模式结构二、中介者模式的特点

SQL Server跟踪自动统计信息更新实战指南

《SQLServer跟踪自动统计信息更新实战指南》本文详解SQLServer自动统计信息更新的跟踪方法,推荐使用扩展事件实时捕获更新操作及详细信息,同时结合系统视图快速检查统计信息状态,重点强调修... 目录SQL Server 如何跟踪自动统计信息更新:深入解析与实战指南 核心跟踪方法1️⃣ 利用系统目录

Windows环境下解决Matplotlib中文字体显示问题的详细教程

《Windows环境下解决Matplotlib中文字体显示问题的详细教程》本文详细介绍了在Windows下解决Matplotlib中文显示问题的方法,包括安装字体、更新缓存、配置文件设置及编码調整,并... 目录引言问题分析解决方案详解1. 检查系统已安装字体2. 手动添加中文字体(以SimHei为例)步骤

SpringSecurity显示用户账号已被锁定的原因及解决方案

《SpringSecurity显示用户账号已被锁定的原因及解决方案》SpringSecurity中用户账号被锁定问题源于UserDetails接口方法返回值错误,解决方案是修正isAccountNon... 目录SpringSecurity显示用户账号已被锁定的解决方案1.问题出现前的工作2.问题出现原因各

基于Python开发Windows屏幕控制工具

《基于Python开发Windows屏幕控制工具》在数字化办公时代,屏幕管理已成为提升工作效率和保护眼睛健康的重要环节,本文将分享一个基于Python和PySide6开发的Windows屏幕控制工具,... 目录概述功能亮点界面展示实现步骤详解1. 环境准备2. 亮度控制模块3. 息屏功能实现4. 息屏时间

使用Python开发一个现代化屏幕取色器

《使用Python开发一个现代化屏幕取色器》在UI设计、网页开发等场景中,颜色拾取是高频需求,:本文主要介绍如何使用Python开发一个现代化屏幕取色器,有需要的小伙伴可以参考一下... 目录一、项目概述二、核心功能解析2.1 实时颜色追踪2.2 智能颜色显示三、效果展示四、实现步骤详解4.1 环境配置4.

使用Python实现获取屏幕像素颜色值

《使用Python实现获取屏幕像素颜色值》这篇文章主要为大家详细介绍了如何使用Python实现获取屏幕像素颜色值,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 一、一个小工具,按住F10键,颜色值会跟着显示。完整代码import tkinter as tkimport pyau