How to gradually increase a float ShaderGraph inspector variable from -1 to 1 through code
Hey guys, for a school project I have to create a game in a team of 5, I'm the game developer and i'm a beginner in Unity.. I do have a little knowledge in coding tho...
currently, I have a dissolve shader using shader graph ( brackeys tutorial ) but I'm trying to dissolve the objects into the screen on keydown (space) and make them disappear using the dissolve shader on a second keydown (space).
currently what happens is when I press space the object shows itself and on the second keydown it disappears again.. i did some googling and came up with the current code but the shader itself isn't transitioning/animating..
(I did also find something about lerp but for some reason, I can't figure that out) I hope that someone knows how to do this, thank you guys in advance
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class mechanicSwitch : MonoBehaviour {
public Material dissolveMat;
public bool build;
// animate the game object from -1 to +1 and back
private float shown = -1f;
private float hidden = 1f;
// Use this for initialization
void Start () {
dissolveMat.SetFloat("_DissolveReference", hidden);
}
// Update is called once per frame
void Update () {
if (Input.GetKey(KeyCode.Space))
{
if (build == false)
{
dissolveMat.SetFloat("_DissolveReference", hidden);
build = true;
}
else
{
dissolveMat.SetFloat("_DissolveReference", shown);
build = false;
}
}
}
}
Answer by babels · Jun 18, 2020 at 08:30 PM
https://www.youtube.com/watch?v=VG-Ux8RHMoA
that is what you want.
You may also want to expose your property with a underscore and call it as public not private. You arn't going to want to multiply the albedo back into like he does. Set up a stand alone vector and run it directly into the alpha, or just procedural destroy the multiply connection and set alpha to 1 to make it fully disappear. In this example you would call the shader sub property not the material itself. Requires a render pipeline asset with opacity turned on.