• About
  • Games
  • Asset Packs
  • 3D Art
  • Environments
  • Game Studies
  • Blog
Stefan Dolidis
  • About
  • Games
  • Asset Packs
  • 3D Art
  • Environments
  • Game Studies
  • Blog
Stefan Dolidis

HIDING BEHIND A WALL? (UNITY & C#)

11/4/2019
Picture
If the platform your building your game to doesn't support a specific custom shader that contains the ability to see the player through a wall, I have an alternate way that may help you with that problem.
​
I had been messing around with a New Nintendo 3DS development kit, and found out that it doesn't support custom shaders. Specifically, a shader I wanted to use which gave the ability to see the player behind a wall, so the actual person playing the game knows where the character is on screen.

This alternate option uses a Linecast from the Camera game object to the position of the Player, and then when the player is behind a wall a sprite pops up on the screen where the player is positioned. You can imagine that the sprite could be some quirky animating image of the face of the main character.

I have a couple of other scripts running during the game, a player controller script, a camera follow, etc. I will only go through the ability to find where the player is when him/her is behind a wall. Also, I am using an orthographic projection on the camera, but not to worry because this alternate way will still work if your camera is in a perspective view. 
​

Full C# Script and Example at bottom of blog post.
Checked with Unity version: 2018.3
First we created a script in Unity, by either right clicking in the Project tab -> Create -> C# Script or by selecting the main camera then in the Inspector; Add Component -> new script, and then name it anything you like, I named the script CameraRayPlayerBehind, I'm sure you can name it with a better and more fitting name.

Open up the script, I'll be using Visual Studio. You can delete the two lines at the top which are:


using System.Collections;
using System.Collections.Generic;
​
And now add:
using UnityEngine.UI;

Now it's time to add everything we need for the mechanic to work. At the top of the script, just before the Start function add:
Picture
Picture
Then back in Unity, create/add a player game object if you haven't yet (e.g. Create -> 3D object -> Cube), create a Canvas (Create -> UI -> Canvas), and name them anything you like.

On that canvas I changed the Render Mode to Screen Space - Camera, and also created another Camera game object that takes care of the UI elements in the game. I then dragged the UI Camera in the Render Camera slot on the Canvas. You can choose to do this or just stick to one camera. (There are no scripts on the UI Camera, just on the Main Camera). If your using two cameras don't forget to uncheck UI on the Culling Mask of the Main Camera and select the UI layer on the Culling Mask on the UI Camera and nothing else. Also make sure the Layer of each game object is correctly displayed just like the pictures below. (Main Camera Layer = Main Camera, Canvas Layer = UI).
Picture
Picture
Picture

Now create an image (Create -> UI -> Image) and add it to your canvas, This is going to be the sprite that shows up on the screen when the player is behind a wall (make sure you choose a sprite for your UI Image).

Select the Main Camera and in the boxes below on the script drag your Image and Canvas to the script.
Picture
After that, it's time to create some layers. On the Player, go to the Layer option in the Inspector tab, and add Layer.
Now add two new Layers, name one Player and the other BehindWall.
Picture
Picture
Make sure you set the Player layer onto the player after creating it. And now create a new game object or if you have a wall in your scene, make sure the layer of that object/wall is set on BehindWall.

You can also open up the Project Settings (Edit -> Project Settings...) and uncheck a couple of things on the Layer Collision Matrix on the Physics tab.
Picture
With that now done, we can go back and select the Main Camera and now on the script we can choose the Player and BehindWall layers on the LayerMask.
Picture
Back to the script. In the Start function add these lines:
Picture
Now let's set up the Linecast. (you can also find out more on Linecasts here .)
Picture
After we have set up the Linecast, we can now use its information. So that if the Linecast is not hitting the Player, which will only happen when a wall (with the Layer set to BehindWall) is between the line and the Player. We then show the sprite, which will follow the player behind the wall.
Picture
When the Player is behind a wall the sprite image will pop up and follow the player as long as you are behind a wall. Another thing we could add just below this piece of code to make it easier to visualise the line is this:
Picture
Now when we press the Play button in the Unity Editor a line will be drawn in the Scene view when selecting the Main Camera.
Picture
Picture
Picture
Full Script

    
P.S./Edit: Instead of enabling and disabling the canvas, you can also just turn the actual Image Gameobject on and off.
0 Comments

Your comment will be posted after it is approved.


Leave a Reply.

    Archives

    May 2022
    January 2022
    August 2021
    July 2021
    June 2021
    May 2021
    April 2021
    November 2020
    October 2020
    March 2020
    February 2020
    December 2019
    November 2019
    October 2019
    September 2019
    August 2019
    July 2019
    June 2019
    May 2019
    April 2019
    March 2019
    February 2019
    January 2019
    October 2018
    September 2018

    Categories

    All
    Coding
    Development
    Games Life
    Game Studies
    Old Blog

    RSS Feed

© ​Stefan Dolidis