- Home /
Change Transparency
Hello,
Im in need of assistance. I came across this code in the unity documentation which turns objects transparent when hit with a raycast, and inserted it into my own game. However im unsure how i would efficiently go back to switching the objects that have been turned transparent back into being solid. Any help would be greatyl appreciated.
Thanks!
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour
{
void Update()
{
RaycastHit[] hits;
hits = Physics.RaycastAll(transform.position, transform.forward, 100.0F);
for (int i = 0; i < hits.Length; i++)
{
RaycastHit hit = hits[i];
Renderer rend = hit.transform.GetComponent<Renderer>();
if (rend)
{
// Change the material of all hit colliders
// to use a transparent shader.
rend.material.shader = Shader.Find("Transparent/Diffuse");
Color tempColor = rend.material.color;
tempColor.a = 0.3F;
rend.material.color = tempColor;
}
}
}
}
Answer by FlaSh-G · Aug 10, 2021 at 03:16 PM
Cache the original material and switch back to it in an appropriate situation.
new private Renderer renderer;
private Material originalMaterial;
private Material transparentMaterial;
private bool isShowingTransparentMaterial = false;
private void Awake()
{
renderer = ...;
originalMaterial = renderer.sharedMaterial;
// Set up the transparent Material here
transparentMaterial = new Material(Shader.Find( ... ));
}
private void Update()
{
var shouldShowTransparentMaterial = Physics.Raycast( ... );
if (shouldShowTransparentMaterial != isShowingTransparentMaterial)
{
if (shouldBeTransparent)
{
renderer.sharedMaterial = transparentMaterial;
}
else
{
renderer.sharedMaterial = originalMaterial;
}
isShowingTransparentMaterial = shouldShowTransparentMaterial;
}
}
Thanks for your reply!
Im unsure how to combine these 2 scripts. This is the current script i have changed from the example to to work for my needs in my game, and it turns the walls that the player goes behind transparent when the raycast is hitting it.
void Update()
{
RaycastHit[] hits;
hits = Physics.RaycastAll(Target.position, -Vector3.forward, 3.5f);
for (int i = 0; i < hits.Length; i++)
{
RaycastHit hit = hits[i];
Renderer rend = hit.transform.GetComponent<Renderer>();
if ((rend) && hit.transform.tag == "Wall")
{
// Change the material of all hit colliders
// to use a transparent shader.
rend.material.shader = Shader.Find("Transparent/Diffuse");
Color tempColor = rend.material.color;
tempColor.a = 0.2F;
rend.material.color = tempColor;
}
}
}
This script is located on the camera object in my scene. How would I go about combining your example with this script? Sorry for the bother but im new to program$$anonymous$$g and c#. As it is once the player moves away the wall stays see through.
Thanks again for the help, it is very appreciated.
Hm yeah, after looking over your current script, it's admittedly a bit complicated to do the changes. The script needs to remember all renderers that it has currently made transparent so as soon as a renderer is not hit anymore, it can be reset to its normal state. Here's the script.
private readonly Dictionary<Renderer, Material> currentlyTransparentRenderers = new Dictionary<Renderer, Material>();
private readonly HashSet<Renderer> currentlyHitRenderers = new HashSet<Renderer>();
private readonly List<Renderer> renderersToRemove = new List<Renderer>();
private void Update()
{
RaycastHit[] hits = Physics.RaycastAll(Target.position, -Vector3.forward, 3.5f);
currentlyHitRenderers.Clear();
renderersToRemove.Clear();
// Make all renderers that we hit transparent if they aren't already
foreach (RaycastHit hit in hits)
{
Renderer renderer = hit.transform.GetComponent<Renderer>();
if (renderer)
{
if (!currentlyTransparentRenderers.ContainsKey(renderer))
{
MakeTransparent(renderer);
}
currentlyHitRenderers.Add(renderer);
}
}
// Reset renderers that we didn't hit
if (hits.Length < currentlyTransparentRenderers.Count)
{
foreach (Renderer renderer in currentlyTransparentRenderers.Keys)
{
if (!currentlyHitRenderers.Contains(renderer))
{
ResetToOriginal(renderer);
}
}
foreach (Renderer renderer in renderersToRemove)
{
currentlyTransparentRenderers.Remove(renderer);
}
renderersToRemove.Clear();
}
}
private Material CreateTransparentMaterialFrom(Material originalMaterial)
{
Material transparentMaterial = new Material(Shader.Find("Transparent/Diffuse"));
Color color = originalMaterial.color;
color.a = 0.2f;
transparentMaterial.color = color;
return transparentMaterial;
}
private void MakeTransparent(Renderer renderer)
{
currentlyTransparentRenderers.Add(renderer, renderer.sharedMaterial);
renderer.sharedMaterial = CreateTransparentMaterialFrom(renderer.sharedMaterial);
}
private void ResetToOriginal(Renderer renderer)
{
renderer.sharedMaterial = currentlyTransparentRenderers[renderer];
renderersToRemove.Add(renderer);
}
WOW THANK YOU SO MUCH. After adding in the hit tag for walls so it only works on them It works great with absolutley no problems and I would not have been able to do that without your awesome script. But I was wondering if it is possible to lerp the transparency so it fades in and out instead of instantly have an alpha of 0.2 or solid? I looked around and tried using the unity reference on Color.Lerp(); and integrating it into your script but couldnt figure out how to get the desired result. I also saw somewhere else that they used MoveTowards for a similar case, but again I couldnt quite figure it out. Are either of these 2 methods correct and an efficient way of doing it?
Thanks again for all your help so far! Reading your scripts is helping me understand the process alot easier :)
Your answer
Follow this Question
Related Questions
Want to move object slowly to where the mouse clicks? 1 Answer
Break Block Like Minectaft 2 Answers
Layer and LayerMask Variables 0 Answers
How do i get the Raycast rotation? 1 Answer