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,再按照原来方法处理就可以。

相关推荐
空中海2 小时前
第二篇:Unity中级阶段(核心开发能力)
unity·游戏引擎
Python私教7 小时前
HermesAgent 终端工具 Windows 兼容性修复实战:两个 Bug 的排查与解决
windows·bug
DaLiangChen8 小时前
Unity 实用工具:动态绘制物体边界包围盒(支持屏幕固定线宽)
unity·游戏引擎
张老师带你学9 小时前
Unity 食物 农产品相关
科技·游戏·unity·游戏引擎·模型
mxwin9 小时前
Unity Custom Interpolators与半透明阴影的原理与实战
unity·游戏引擎·shader
晴夏。9 小时前
UE5第三人称模板实现及相关引擎源码分析
unity·ue5·游戏引擎·ue
HAPPY酷9 小时前
解决 Unreal Engine 编译报错 MSB4018:三个核心排查方向
游戏引擎·虚幻
瀚高PG实验室10 小时前
pgroonga全文检索插件的BUG
数据库·postgresql·bug·瀚高数据库
晴夏。13 小时前
UE原生MovementBase实现分析
游戏引擎·ue·3c
天人合一peng14 小时前
Unity工程发布hololens需安装, MRTK安装
unity·游戏引擎·hololens