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

相关推荐
mxwin9 小时前
Unity URP 法线贴图如何生成 用什么工具创建
unity·游戏引擎·贴图
mxwin20 小时前
Unity URP 法线贴图色彩空间、编码与解码
unity·游戏引擎·贴图·shader
玖玥拾1 天前
Cocos学习笔记:项目框架搭建与异步加载进度
游戏引擎·cocos2d
mxwin1 天前
Unity Shader URP:将法线可视化,便于调试
unity·游戏引擎·shader
蓝黑墨水1 天前
unity相关链接
unity·游戏引擎
mxwin1 天前
Unity Shader 法线贴图的七种错误用法
unity·游戏引擎·贴图·shader
mxwin1 天前
Unity URP 切线空间详解
unity·游戏引擎·shader
绘梨衣5471 天前
采集基类设计遇到的描述符bug
爬虫·python·bug
不吃鱼的羊1 天前
达芬奇工具Bug
bug
caimouse1 天前
Godot Engine 最新版官方文档(简体中文完整翻译 & 精简梳理)
游戏引擎·godot