0
\$\begingroup\$

I'm trying to make a isometric game. I want to make it so that when the player goes behind a wall, the wall's alpha goes down.

How do I go about doing this?

I have this code that I made, but I don't understand what's wrong with it.

I already have the wall that's meant to disappear on the "Wall" layer.

using UnityEngine;
using System.Collections;

public class WallAlpha : MonoBehaviour {

    public LayerMask mask =LayerMask.GetMask("Wall");

    void Update() {

        if (Physics.Raycast(transform.position, transform.forward, 100, mask.value))
        {
            transform.GetComponent<Renderer>().material.SetColor("_Color", new Color(1,1,1,0.1f));
            Debug.Log("Something in the way");
        }

        else
        {
            transform.GetComponent<Renderer>().material.SetColor("_Color", new Color(1,1,1,1.0f));
            Debug.Log("Nothing In the Way!");
        }
    }
}
\$\endgroup\$
4
  • \$\begingroup\$ What shader are you using, and what kind of renderer is this? \$\endgroup\$
    – jgallant
    Commented Jan 31, 2016 at 16:00
  • \$\begingroup\$ It's a mesh renderer. I'm not sure about the shaders. I'm very new to Unity, but I don't think there are any shaders. The object I'm trying to work this upon is an empty box GameObject. I'm really sorry I can't provide more info. \$\endgroup\$ Commented Jan 31, 2016 at 16:36
  • \$\begingroup\$ If you look at the material, there should be a shader assigned to it. If not, then you are probably using the standard shader. Basically, I wanted to ensure you are calling the correct property. Most cases, you have to check the shader source code (downloaded here: unity3d.com/get-unity/download/archive) and look at the property names. \$\endgroup\$
    – jgallant
    Commented Jan 31, 2016 at 17:00
  • \$\begingroup\$ Would an image help explain? The shader is set to Default. prntscr.com/9xa9e6 I downloaded the built-in shaders package, but I'm unsure what to do next. I understand some workings of Unity, but this is, unfortunately, out of my ken. I'm really sorry for all the trouble, and thanks a bunch. \$\endgroup\$ Commented Jan 31, 2016 at 18:18

1 Answer 1

2
\$\begingroup\$

Actually Unity create a default material for meshes if you don't assign any material explicitly. And the Rendering Mode of that default material is set to Opaque that prevents you to play with transparency. Although _Color property is declared in Standard Shader. Means your code is right. Well, it's totally another topic. What you can do is to create your own material, set its Rendering Mode to Transparent and drop it on your mesh.

1- Create a New Material.

New Material

2- Set it's Rendering Mode to Transparent.

Rendering Mode

3- Drop that material on your mesh.

Now you can play with transparency.

\$\endgroup\$
3
  • \$\begingroup\$ Very sorry for the late reply, some things came up so I couldn't check out your answer and try it out in the past few weeks. I'll try your suggestion out and report back as soon as I can, thanks. \$\endgroup\$ Commented Feb 9, 2016 at 3:04
  • \$\begingroup\$ I've managed to try it out, and I'd like to thank you. It works, but I'm having difficulty in finding out where the raycast hits. \$\endgroup\$ Commented Feb 11, 2016 at 10:13
  • 1
    \$\begingroup\$ You should ask a separate question for that, including all the details related to your issue. So all the members of community would focus on your new problem. If this works then marking it answer would be a highly appreciable act. \$\endgroup\$ Commented Feb 11, 2016 at 10:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .