- Home /
Top-Down Shooter, see through walls?
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
Answer by $$anonymous$$ · Aug 05, 2012 at 10:44 PM
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.
although I will not be using your technique I'd like to thank you for an idea, I'll write a shader to draw the world objects that takes in the players Projected Z and then compares it to the world objects projected Z, if its in the way a black and white image will be sampled for the alpha which will basicly be a blurred circle. This will make a peep hole effect when moving under things.
That sound like a very elegant solution. When you manage to implement it, if you could share a project file with us it would be great. But yeah, nice solution...
here you go http://pastebin.com/5mY$$anonymous$$hp4h you'll need to change the height check for anything other than a top down shooter, also because of how unity treats transparent objects you must split all objects effected by this if their going to noticabley overlap. EDIT: Actually I just realised you can not have overlapping objects, transparency has no Z-buffer in unity and so a lot of flattening occurs.
would this script youve linked to here be used as a component attached to the main camera?
Thank you!!
Answer by toorican · Nov 14, 2021 at 11:41 AM
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 .
https://forum.unity.com/threads/rel...isible-within-buildings-std-urp-hdrp.1182130/ https://assetstore.unity.com/packages/vfx/shaders/see-through-shader-193955
Greetings Toorican
Your answer
![](https://koobas.hobune.stream/wayback/20220613075049im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Top Down shooter Moving while Shooting 0 Answers
Shooting a projectile with Unet 0 Answers
Instantiate gameobject on random position on 1. a graph and 2. another gameobject 0 Answers
How can I make walls with Collider2D components for a top-down game in Unity 2018? 1 Answer
Scrolling space shooter - making enemies drop currency 1 Answer