Hi, i am trying to lerp between 2 colors using code to access the emission of the particle vertexlit shader, at the moment i am having a problem even just accessing it to change the color for a test
i have tried using _EMISSION, _emisColor, _emissionColor etc but when run it just turns pink and the shader turns to an hidden internal error shader as in the picture below, i have checked the shader and it says _emisColor under properties in the shader, but it will not work
the material is just applied to a standard cube
i am using unity 2018.1.6
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LERPCOLOUR : MonoBehaviour {
// Use this for initialization
void Start () {
Renderer rend = GetComponent<Renderer>();
rend.material.shader = Shader.Find("_EmisColor");
rend.material.SetColor("_EmisColor", Color.green);
}
// Update is called once per frame
void Update () {
}
}
Why are you replacing the shader on the material via Shader.Find? That’s what is causing your error and the invalid material when you run the project.
Can anyone help with this, i currently have the code below, it seems to basically work but there are 2 problems that i just can not seem to work out,
1, i want to use seconds for the lerp time, but at the moment i am having to use 1000f just to get about 3 seconds, how can i enter it in seconds, if i shorten to say 10f it then starts yellow and ends green, but there is no red at begining, using the debug.log you can see it taking ages to reach 1., if shorten the time it does not lerp smooth, the red to yellow is quick, then seems to be on yellow for ages, then at end quick yellow to green, the middle section is very long, i need the same speed all the way through. i am really stuck
2, i am trying to lerp from red to green as you can see in the rgb values that i have put in, but when run it goes from red to yellow then takes really ages to reach green.
any help much apreciated thank you
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NEWCOL4 : MonoBehaviour {
Material thisMat;
float lerpTime = 1000f;
float currentLerpTime;
Color oldColor = new Color(255, 0, 0, 1.0f);
void Start () {
thisMat = GetComponent<Renderer>().material;
thisMat.EnableKeyword("_EMISSION");
}
void Update () {
currentLerpTime += Time.deltaTime;
if (currentLerpTime > lerpTime) {
currentLerpTime = lerpTime;
}
float perc = currentLerpTime / lerpTime;
thisMat.SetColor("_EmisColor", Color.Lerp(oldColor, new Color(0,255,0, 1.0f),perc));
Debug.Log(perc);
}
}
Well it takes forever because you have it set to 1000 seconds at the top. Change that to 5 or 10 if you want it to only take 5 or 10 seconds. If you want it to continuously cycle instead of being only one way, use something like Mathf.PingPong: Unity - Scripting API: Mathf.PingPong
NO NO NO, you did not get what i mean, it does not seem to represent seconds is what i am saying, if i put in 10.0f it takes about 100 milisecods, if i put 1000.0f it takes about 20 seconds, it is not using the input as seconds which is why i need help, it is running the lerp way too fast and not representing seconds.
Your lerp is 100% defined in seconds.
float perc = currentLerpTime / lerpTime;
perc = 0.5 seconds / 2 seconds = 0.25 = 25%
perc = 1.0 seconds / 2 seconds = 0.50 = 50%
perc = 1.5 seconds / 2 seconds = 0.75 = 75%
perc = 2.0 seconds / 2 seconds = 1.00 = 100%
Perhaps your Time.timeScale isn’t 1.0 which would change the timings.
i am now even more confused!!! , i am using a new project so i have not messed with the time scale, i just can not get the lerp value to = real seconds, is there any other way i could code a lerp to get it right as it is confusing the hell out of me. thank you
Good news, I found your issue. I copy and pasted your code and you’re correct, everything stays yellow until the very end when it suddenly switches to green. The problem isn’t your lerp though. I overlooked that you’re using the Color object, which takes float values in the range of 0…1 to define the colour, and it’s Color32 that uses the more traditional 0…255 range bytes. So either change your current code to use 1,0,0 for red and 0,1,0 for green or convert all of your Color objects to Color32 and use 255,0,0 for red and 0,255,0 for green.