I’m making a top down shooter, nothing too fancy, but I face a problem when the player goes under a bridge or a tunnel, which is the player can’t see themselves or anything around them.
The bridges and or tunnels will be traversable over and under (under obviously being the problem) Is there any way I could get a gradient circle controlling the alpha of all that’s drawn above the player?
Any and all help is greatly appreciated,
Thanks in advanced,
Bombshell
I would approach this problem like this. Have a ray casting from camera to the player. Check if something gets hit from that ray cast. If it does check if its the Bridge. If it is the bridge, then make it transparent.
So for this to work, you have to create a material and set its shader to be Transparent/Diffuse, and assign it to the bridge. And attach the following script to Bridge as well.
using UnityEngine;
using System.Collections;
public class Bridge : MonoBehaviour
{
public Color transparentColor;
private Color m_InitialColor;
void Start ()
{
m_InitialColor = renderer.material.color;
}
public void SetTransparent()
{
renderer.material.color = transparentColor;
}
public void SetToNormal()
{
renderer.material.color = m_InitialColor;
}
}
After doing that create a new GameObject and add the following script to it. Oh and drag and drop the Player transform to ‘playerTrans’ property on the inspector as well…
using UnityEngine;
using System.Collections;
public class ObstructionDetector : MonoBehaviour
{
public Transform playerTrans;
private Bridge m_LastBridge;
void Start ()
{
StartCoroutine(DetectPlayerObstructions());
}
IEnumerator DetectPlayerObstructions()
{
while(true)
{
yield return new WaitForSeconds(0.5f);
Vector3 direction = (playerTrans.position - Camera.mainCamera.transform.position).normalized;
RaycastHit rayCastHit;
if(Physics.Raycast(Camera.mainCamera.transform.position, direction, out rayCastHit, Mathf.Infinity))
{
Bridge bridge = rayCastHit.collider.gameObject.GetComponent<Bridge>();
if(bridge)
{
bridge.SetTransparent();
m_LastBridge = bridge;
}
else
{
if(m_LastBridge)
{
m_LastBridge.SetToNormal();
m_LastBridge = null;
}
}
}
}
}
}
I’ve made a small video tutorial to show how this is done.
Hi
If your still looking for a solution check our Asset as this is custom made shader for this use case.
It can handle multi user multi buildings ( obstructions ) and is available for Standard URP and HDRP .