Recently i had to start working on a prototype of a new project in Unity, but i only understand the very basics of scripting and started doing this a few days ago.
So, i want my script to activate the renderer of object A and let it fade in if objectB enters its collider.
And if objectB leaves the collider, objectA shall fade out and deactivate its renderer again.
Object A is always a plane with a video or normal texture on it.
Everything works right now, except the deactivation of the renderer.
Also, if you guys could tell me how i could use the collider of a 3. object to do this i would be very happy. (if B enters C = A fades in / if B leaves C = A fades out)
This would help me being able to adjust the collider much faster, because i found its faster to reposition a game object instead of a collider. Collider handles are a bit more annoying!
and heres the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class planeFade : MonoBehaviour {
bool fadeout = false;
Renderer myRenderer;
void Start ()
{
myRenderer = GetComponent<Renderer>();
}
void Update ()
{
if (fadeout) {
myRenderer.material.color = Color.Lerp (myRenderer.material.color, new Color (myRenderer.material.color.r, myRenderer.material.color.g, myRenderer.material.color.b, 1f), 1 * Time.deltaTime);
}else{
myRenderer.material.color = Color.Lerp(myRenderer.material.color, new Color(myRenderer.material.color.r, myRenderer.material.color.g, myRenderer.material.color.b, 0f), 1*Time.deltaTime);
}
if (myRenderer.material.color.a == 0f) {
myRenderer.enabled = false;
}
}
private void OnTriggerEnter(Collider other)
{
myRenderer.enabled = true;
fadeout = true;
}
private void OnTriggerExit(Collider other)
{
fadeout = false;
}
}
Ah I see that you’re leaping wrong, also you don’t need 2lerps for this.
Leaping works like this:
you have two values A and B and you have one float lets call it t that should go from 0.0 to 1.0
If you lerp is lerp(A,B,t), when t = 0, your value will be 100% A, when t = 1, the value is 100% B
so t at 0.5 gives 50%A + 50%B.
Time.deltaTime just gives you how long it toke to render this frame, so 1*Time.deltaTime mostly gives you something around 0.03
For your lerp you should do something like
float lerp;
void Update()
{
lerp += Time.deltaTime;
Mathf.Clamp01(lerp);
Color c = Color.Lerp(A,B,lerp);
}
And so lerping from 1 to 0 should give you the inverse effect.
How do you start? With your renderer disabled, because I’m not sure if getcomponent works on disabled components.
Maybe set it public for debugging?
If you changed the lerp function and the 0 check to <=, then check if the materials alpha is actually changing.
Maybe the lerp brings it to 0.00something, then just check if <= 0.1 orso…
still your lerp isn’t what i should be, Debug 1*Time.deltaTime and you will see that the value isn’t incrementing. at best it’s just twitching around some value.
you really should try doing something like this:
Thanks again, i really appreciate your help!
I’m trying to get this running, but unfortunately the 1% unsureness resulted in 2 mistakes (at least 2 i think i figured out).
The first one is easy to fix: its “Mathf”, not just “Math” in line 19.
The 2nd one is that you should switch line 10 and 11, or you get a “NullReferenceException”. I hope i understood it right and it was the right way to fix this one.
So now i get no errors, but i also found out that the script doesnt seem to work like this. Nothing happens, except that the renderer now deactivates when i enter the collider. And this isnt exacly what i was looking for.
put this on your trigger and give the planeToFade variable the gameObject you want to fade in and out.
make sure you set the shader of the plane to fade.