Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Vire · Jan 29, 2014 at 03:40 AM · camerashadermaterialspherecast

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;
     }
 }
 

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges