Adjust Trees transparency/Opaque/Alpha value
I've made the following script that determines if something is blocking the cameras view of the player
if (Physics.Raycast(player.position, targetMove - player.position, out hit, Vector3.Distance(player.position, targetMove)))
{
//if the last raycast hit is not the same as the new one (E.g. Monster to rock) set Monster to 100% transparency (can see)
if (hit.transform.gameObject.tag != "Terrain")
{
if (oldGameobject.name != hit.transform.gameObject.name)
{
oldGameobject = hit.transform.gameObject;
color.a = 1;
}
//Get render of the Gameobject
render = hit.transform.gameObject.GetComponentInChildren<Renderer>();
mat = render.material;
color = mat.color;
//If alpha value is greater then subtract
if (color.a > 0.7f)
{
color.a -= 0.1f;
}
mat.color = color;
//targetMoveUse = Vector3.Lerp(hit.point, player.position, rayHitMoveInFront);
}
Now this works on one of my monster characters using a custom shader, (Textures don't allow adjusting the opaque/alpha value by default) As you can see Below the monster is slightly Transparent (you can see the players legs)
Now here is the tree which isn't transparent, any help would be greatly appreciated
(I got the Idea from a game called Titan Quest)
Answer by JaredHD · Dec 16, 2016 at 01:25 PM
I've figured it out. All ownership of the code goes to Panthesilea, http://answers.unity3d.com/questions/694727/unity-tree-transparency.html
Here is the code that made it work but it requires editing unity's default trees. i deleted the LODs in the tree and picked one of the trees (child of the tree and made it the parent), I then created a raycast from the camera to the player and determined if something was in front of the camera and if the tag was equal to a tree I then ran the following code.
private void SetTransparent(GameObject g)
{
for (int i = 0; i < g.renderer.materials.Length; i++)
{
g.renderer.materials[i].shader = Shader.Find("Transparent/Diffuse");
g.renderer.materials[i].SetColor("_Color", new Color(1, 1, 1, 0.1f));
}
for (int i = 0; i < g.transform.childCount; i++)
SetTransparent(g.transform.GetChild(i).gameObject);
}
Answer by randytayler · Mar 04, 2017 at 03:48 PM
I'm having trouble with mobile, the same one described by @Panthesilea. My trees are all turning purple when I try to restore them to opaque after having made them transparent. He mentions using a Resources folder, but I don't follow -- and his post is pretty old. Did you have to solve that problem, @JaredHD?
There were 2 ways to do it if i remember correctly. Create a folder caller Resources and place it in there or what i did was go to "Edit->Project Settings->Graphics." Inside there, you'll find something called "Always included shaders". Add your shader to that