You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

俯视角游戏中相机与玩家间遮挡物透明化实现方案问询

俯视角游戏遮挡物透明化方案求助(URP管线)

几乎所有俯视角游戏都具备这样的机制:当物体处于相机与玩家之间时,该物体会变为透明。我正在开发一款俯视角游戏,此机制十分关键。

现有遮挡物检测脚本(功能正常但需优化)

public Camera playerCamera;
public Transform playerTransform;

public float sphereCastRadius = 0.1f;
public LayerMask obstructorsLayer; // 可被透明化的物体层


void Update()
{
    foreach (GameObject go in ObstructorsGOs())
    {
        if (go.GetComponent<Renderer>() != null)
            go.GetComponent<Renderer>().material.color = Color.red * new Color(1, 1, 1, 0.5f);
    }

    foreach (GameObject go in NonObstructorsGOs())
    {
        if (go.GetComponent<Renderer>() != null)
            go.GetComponent<Renderer>().material.color = Color.white;
    }
}

List<GameObject> ObstructorsGOs()
{
    List<GameObject> gameObjects = new List<GameObject>();

    Vector3 rayDirection = playerCamera.transform.forward;
    float maxDistance = Vector3.Distance(playerTransform.position, playerCamera.transform.position);
    RaycastHit[] hits = Physics.SphereCastAll(transform.position, sphereCastRadius, rayDirection, maxDistance, obstructorsLayer);

    foreach (RaycastHit hit in hits)
    {
        gameObjects.Add(hit.collider.gameObject);
    }        

    return gameObjects;
}

List<GameObject> NonObstructorsGOs()
{
    List<GameObject> gameObjects = new List<GameObject>();

    foreach (GameObject go in FindObjectsOfType<GameObject>())
    {
        if (!ObstructorsGOs().Contains(go))
        {
            gameObjects.Add(go);
        }
    }

    return gameObjects;
}

遇到的问题

我目前通过修改材质颜色并调整alpha值来实现透明效果,但存在以下问题:

  • 当材质Surface Type为Opaque时,透明效果完全无效
  • 设置为Transparent时,出现无法解释的异常效果(附效果GIF)

需求

我使用的是URP渲染管线,希望找到能实现类似《The Ascent》中遮挡物透明效果的可行方案,同时也欢迎对现有检测脚本的优化建议。

内容的提问来源于stack exchange,提问作者Obscure021

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 01:23:29