- Home /
Camera Fade Script - Works only in editor
Hi there, recently I asked for help with a camera fade script and it worked ok, now I've fixed it up a lot and everything was perfect except when I build the game instead of fading out it turns pink - the color meaning that there's no shader assigned.
I think what is happening is that since no object is assigned "Diffuse with shadow" (a custom shader that I got off the internet, that allows transparent objects to have shadows) it doesn't build it, so when it's assigned it doesn't exist in the build.
I use SphereCast to see if the camera can see the player, then store everything blocking the view, then assign another script to those objects blocking the view that causes them to fade out then cast back at the camera to see if they are still hitting it. Expecting the camera to be the only raycast didn't work right as the objects don't fade back in on rare occurrences.
How can I fix this?
Here is the code:
This Attaches to Camera
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class CameraFaderAuto : MonoBehaviour
{
public GameObject target;
public GameObject player;
public float fadeAmount = 0.2f;
public float fadeSpeed = 5f;
private LayerMask layerMask;
private RaycastHit[] hits;
private GameObject[] storedHits;
private HashSet<GameObject> hitsGO = new HashSet<GameObject>();
void Start()
{
player = GameObject.FindGameObjectWithTag("Player");
target = GameObject.FindGameObjectWithTag ("CamTarget");
if(target == null)
{
Debug.LogError("No camera target detected, please correct this issue and restart");
}
}
void Update()
{
layerMask = 1 << 8;
float distance = Vector3.Distance(target.transform.position, transform.position);
Vector3 direction = (new Vector3(target.transform.position.x, target.transform.position.y - 1f, target.transform.position.z)) - transform.position;
float radius = player.GetComponent<CharacterController>().height / 2.5f;
Ray ray = new Ray(transform.position, direction);
hits = Physics.SphereCastAll(ray, radius, distance, layerMask);
hitsGO.Clear();
foreach(RaycastHit h in hits)
{
if(h.transform.gameObject.renderer == null)
continue;
AutoFaderSelfDetect fader = h.transform.gameObject.GetComponent<AutoFaderSelfDetect>();
if(fader == null)
fader = h.transform.gameObject.AddComponent<AutoFaderSelfDetect>();
fader.MakeTransparent();
}
}
}
If SphereCast hits, attaches to object
using UnityEngine; using System.Collections;
public class AutoFaderSelfDetect : MonoBehaviour { public bool makeOpaque = false;
private Shader originalShader = null;
private Color originalColor = Color.black;
private float transparency = 0.3f;
private const float targetTransparency = 0.2f;
private const float transparencySpeed = 8f;
public GameObject target;
public GameObject player;
public float fadeAmount = 0.2f;
public float fadeSpeed = 5f;
private LayerMask layerMask;
private RaycastHit hit;
void Start()
{
player = GameObject.FindGameObjectWithTag("Player");
target = GameObject.FindGameObjectWithTag ("CamTarget");
if(target == null)
{
Debug.LogError("No camera target detected, please correct this issue and restart");
}
}
public void MakeTransparent()
{
transparency = targetTransparency;
if(originalShader == null)
{
originalShader = renderer.material.shader;
originalColor = renderer.material.color;
renderer.material.shader = Shader.Find("Transparent/Diffuse with Shadow");
}
}
void Update()
{
if(transparency < 1.0f && !makeOpaque)
{
Color color = renderer.material.color;
color.a = transparency;
renderer.material.color = new Color(color.r, color.g, color.b, Mathf.Lerp (renderer.material.color.a, color.a, Time.deltaTime * transparencySpeed));
}
if(makeOpaque)
{
Color color = renderer.material.color;
color.a = 1.0f;
renderer.material.color = new Color(color.r, color.g, color.b, Mathf.Lerp(renderer.material.color.a, color.a, Time.deltaTime * transparencySpeed));
//transparency += ((1.0f - targetTransparency) * Time.deltaTime) / transparencyTimeout;
if(renderer.material.color.a >= 0.97)
{
renderer.material.shader = originalShader;
renderer.material.color = originalColor;
Destroy(this);
}
}
layerMask = 1 << 8;
float distance = Vector3.Distance(target.transform.position, Camera.main.transform.position);
Vector3 direction = (new Vector3(target.transform.position.x, target.transform.position.y - 1f, target.transform.position.z)) - Camera.main.transform.position;
float radius = player.GetComponent<CharacterController>().height / 2.5f;
Ray ray = new Ray(Camera.main.transform.position, direction);
if(Physics.SphereCast(ray, radius, distance, layerMask) != this.transform)
{
makeOpaque = true;
}
}
}
Your answer
Follow this Question
Related Questions
LineRenderer not showing in certain angles 1 Answer
Material that punches transparent hole in camera's view? 0 Answers
Image Effect lighting 0 Answers
Using a camera replacement shader but keeping the underlying (original) colors? -4 Answers
Is there a shader in Unity that prevents transparent parts from overlapping? 1 Answer