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

相关推荐
又来0771 小时前
Unity手柄按键映射表
unity·游戏引擎
dzj20211 小时前
Unity的旁门左道用法(科学计算):用shader调用GPU做k线MA5的计算——DuckDB + Compute Shader
unity·金融·gpu·shader·量化·compute shader
世洋Blog2 小时前
Unity使用Luban的一些常用点
unity·游戏策划·luban
nnsix14 小时前
Unity的dllType Native和Managed的区别
unity·游戏引擎
编码旅者15 小时前
《Virt A Mate(VAM)》免安装豪华版v1.22中文汉化整合
游戏引擎·图形渲染·vr·动画
Clank的游戏栈16 小时前
AI游戏开发全自动编程课程体系(Cursor版,支持Unity/Cocos, Laya后续支持)
人工智能·unity·游戏引擎
鹿野素材屋17 小时前
技术闲聊:为什么网游会在固定时间点,刷出固定的道具?
前端·网络·unity
发际线危机1213818 小时前
Unity发布apk部分真机虚线采样变成实线问题
unity·游戏引擎
FVV112318 小时前
电脑录屏工具Bandicam 无时长限制,支持4K画质
eclipse·游戏引擎·ar·动画·ogre
猫不吃咸鱼20 小时前
Unity中攻击检测敌人的多种方法
游戏·unity·c#·游戏引擎