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

相关推荐
2301_767113986 小时前
Superpowers 游戏引擎从零开发实战指南
游戏引擎
吴梓穆7 小时前
untiy TextMeshPro (TMP) text 操作 中文字符集 字体材质操作(添加纹理 添加描边 添加阴影)
unity
想你依然心痛10 小时前
嵌入式单元测试:Unity/CMock框架与硬件在环测试——测试驱动、桩函数
unity·单元测试·游戏引擎
yangmu320311 小时前
《星露谷物语》MOD配置与实战安装综合指南
游戏·游戏引擎·游戏程序
xcLeigh11 小时前
Unity基础:Game视图详解——游戏预览、分辨率模拟与性能显示
游戏·unity·游戏引擎·音频·视频·game·play模式
ZJU_fish199611 小时前
全局光照/阴影的几个常见问题
游戏引擎·图形渲染
IT·陈寒13 小时前
Superpowers 游戏引擎核心应用场景与落地指南
游戏引擎
xcLeigh21 小时前
Unity基础:Scene视图操作完全指南——视角控制、物体选择与场景导航
unity·游戏引擎·scene·试图·场景导航
mxwin1 天前
Unity Shader exp 函数的算法与渲染应用
算法·unity·游戏引擎·shader
mxwin1 天前
Unity URP Exposure曝光原理与实战应用
unity·游戏引擎