Issues with Mathf.MoveTowards
Hi, I'm having issues with Mathf.MoveTowards. I want to interpolate between 1 and 0 over time with a public float "fadeValue". But the console yields jumpy results from 1, 0, randomly increasing and decreasing till it reaches 0 eventually.
using UnityEngine; using System.Collections;
public class CloudBehaviour : MonoBehaviour {
public float cloudSpeed = 1;
public float cloudScaleMin = 1;
public float cloudScaleMax = 1;
[Range(0f, 355f)]
public float rotationThreshould;
public float fadeValue = 10;
private MeshRenderer meshrenderer;
private float cloudScale;
private float fade;
// Use this for initialization
void Start () {
meshrenderer = GetComponent<MeshRenderer>();
cloudScale = Random.Range(cloudScaleMin, cloudScaleMax);
transform.localScale += new Vector3(cloudScale, cloudScale, cloudScale);
transform.Rotate(0f, Random.Range(0f, rotationThreshould), 0f);
}
// Update is called once per frame
void Update () {
transform.position += new Vector3(cloudSpeed * Time.deltaTime, 0f, 0f);
fade = Mathf.MoveTowards(1f, 0f, fadeValue * Time.deltaTime);
meshrenderer.material.SetFloat("_AlphaLevel", fade);
Debug.Log(fade);
}
}
Lowering the fade Value worked. But, I find that really strange.
The last parameter is the allowed maxDelta towards the target value. Probably you should just use the simpler approach without using that method.
Answer by ScaniX · Aug 23, 2016 at 12:19 PM
MoveTowards should be called on the current value, you keep calling it on the value 1. Try the following instead. Your script is supposed to immediately fade it out to 0 and stay there?
public class CloudBehaviour : MonoBehaviour {
public float cloudSpeed = 1;
public float cloudScaleMin = 1;
public float cloudScaleMax = 1;
[Range(0f, 355f)]
public float rotationThreshould;
public float fadeValue = 10;
private MeshRenderer meshrenderer;
private float cloudScale;
private float fade = 1f;
// Use this for initialization
void Start () {
meshrenderer = GetComponent<MeshRenderer>();
cloudScale = Random.Range(cloudScaleMin, cloudScaleMax);
transform.localScale += new Vector3(cloudScale, cloudScale, cloudScale);
transform.Rotate(0f, Random.Range(0f, rotationThreshould), 0f);
}
// Update is called once per frame
void Update () {
transform.position += new Vector3(cloudSpeed * Time.deltaTime, 0f, 0f);
fade = Mathf.MoveTowards(fade, 0f, fadeValue * Time.deltaTime);
meshrenderer.material.SetFloat("_AlphaLevel", fade);
Debug.Log(fade);
}
}
Thanks! I will try what you've mentioned. I want it to fade to zero over time and then destroy gameobject when its zero.
Okay! I tried it, but the Object fade goes to zero instantly and not over the fadeValue * Time.deltaTme mentioned.
Did you do both changes?
private float fade = 1f;
and
fade = $$anonymous$$athf.$$anonymous$$oveTowards(fade, 0f, fadeValue * Time.deltaTime);
If yes: you probably need to lower the value of fadeValue.
Yes! I made both changes. I tried with fadeValue 10, and 100. for the fade to happen over 10, and 100 secs. What ma I doing wrong? :( I will try lowering the fadeValue.
Your answer
Follow this Question
Related Questions
Transparency / Fade Material lighing issues 0 Answers
IOS does not render `fade` rendering mode 1 Answer
How I can make whole gameobject transparent? 0 Answers
Leaves Transparency 0 Answers