unity开发中使用射线交互物体修改TextMashPro出现镜头抖动及解决

在制作一些类似生存游戏的时候,有时候可能会有光标所指处显示物体名称的需求

以下是相关代码

csharp 复制代码
using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class SelectionManager : MonoBehaviour
{
    public GameObject interaction_Info_UI;
    TextMeshProUGUI interaction_text;
    // Start is called before the first frame update
    private void Start()
    {
        interaction_text = interaction_Info_UI.GetComponent<TextMeshProUGUI>();
    }

    // Update is called once per frame
    void Update()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        if (Physics.Raycast(ray, out hit))
        {
            var selectionTransform = hit.transform;

            if (selectionTransform.GetComponent<InteractableObject>())
            {
                interaction_text.text = selectionTransform.GetComponent<InteractableObject>().GetItemName();
                interaction_Info_UI.SetActive(true);
            }
            else
            {
                interaction_Info_UI.SetActive(false);
            }
        }
    }
}

运行游戏的时候偶然发现一个bug,当屏幕中心光标指向相关物体的时候,会发生屏幕抖动和瞬移的情况。

自己摸索了一个解决方法

1.找到你的摄像头

2.在inspector中,修改Far为任意其他值,比如默认1000设为2000,会发现游戏运行后不会出现抖动情况,而再将2000修改为1000后,游戏依旧可以正常运行。

注:在修改TextMeshPro任意设置后,依旧会出现镜头抖动的bug,再按照原来方法处理就可以。

相关推荐
Magnum Lehar8 小时前
vulkan游戏引擎的platform文件实现
游戏引擎
海 月13 小时前
Unity EventCenter 消息中心的设计与实现
unity·消息中心
虾球xz15 小时前
游戏引擎学习第307天:排序组可视化
c++·学习·算法·游戏引擎
虾球xz15 小时前
游戏引擎学习第306天:图结构排序的调试
c++·学习·算法·游戏引擎
北冥没有鱼啊1 天前
UE 材质几个输出向量节点
ue5·游戏引擎·ue4·虚幻·材质
为你写首诗ge1 天前
【Unity网络编程知识】Unity的 UnityWebRequest相关类学习
网络·unity
猫头虎-前端技术1 天前
如何解决鸿蒙应用闪退问题
华为·typescript·npm·node.js·bug·html5·harmonyos
Dr.勿忘1 天前
Unity异步加载image的材质后,未正确显示的问题
ui·unity·c#·游戏引擎
虾球xz1 天前
游戏引擎学习第301天:使用精灵边界进行排序
学习·算法·游戏引擎
虾球xz1 天前
游戏引擎学习第304天:构建与遍历图
c++·学习·算法·游戏引擎