Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to know is there a way of using Mathf.SmoothStep to control the float of a variable? You see I downloaded this package on unity assets store and created a IEnumerator OnTriggerEnter2D (in a new script) to control the effect once my player hits an orb, making it slow down but now I want to add an animation. Rather than having it appear of the screen and then disappearing, I want to make the frost effect slowly come in, kinda like controlling the frost amount in the inspector. It is possible, because I tried doing it like this:

(This is attach to orb)

// Use this for initialization
void Start () { 
}

IEnumerator OnTriggerEnter2D(Collider2D other)
{
    if (other.tag == "Player") {
        //frost.enabled = true; 
        GameObject.Find("Main Camera").GetComponent<FrostEffect>().enabled = true;
        FrostEffect.valToBeLerped = Mathf.SmoothStep(0, 1f, FrostEffect.FrostAmount);
        yield return new WaitForSeconds (1.01f);
        FrostEffect.valToBeLerped = Mathf.SmoothStep(1, 0, FrostEffect.FrostAmount);
        GameObject.Find("Main Camera").GetComponent<FrostEffect>().enabled = false;
    }
}

But it's still appearing automatically rather than it slowly coming in like an animation. Anyway this is the frost effect script I downloaded from the assets store:

(Attach to the main camera)

public static float FrostAmount = 0.3f; //0-1 (0=minimum Frost, 1=maximum frost)
public static float valToBeLerped = 0;
public float EdgeSharpness = 1; //>=1
public float minFrost = 0; //0-1
public float maxFrost = 1; //0-1
public float seethroughness = 0.2f; //blends between 2 ways of applying the frost effect: 0=normal blend mode, 1="overlay" blend mode
public float distortion = 0.1f; //how much the original image is distorted through the frost (value depends on normal map)
public Texture2D Frost; //RGBA
public Texture2D FrostNormals; //normalmap
public Shader Shader; //ImageBlendEffect.shader

private Material material;

private void Awake()
{
    material = new Material(Shader);
    material.SetTexture("_BlendTex", Frost);
    material.SetTexture("_BumpMap", FrostNormals);
}

private void OnRenderImage(RenderTexture source, RenderTexture destination)
{
    if (!Application.isPlaying)
    {
        material.SetTexture("_BlendTex", Frost);
        material.SetTexture("_BumpMap", FrostNormals);
        EdgeSharpness = Mathf.Max(1, EdgeSharpness);
    }
    material.SetFloat("_BlendAmount", Mathf.Clamp01(Mathf.Clamp01(FrostAmount) * (maxFrost - minFrost) + minFrost));
    material.SetFloat("_EdgeSharpness", EdgeSharpness);
    material.SetFloat("_SeeThroughness", seethroughness);
    material.SetFloat("_Distortion", distortion);
    ///Debug.Log("_Distortion: "+ distortion);

    Graphics.Blit(source, destination, material);
}

Thank you. :)

Second Edit Because so far this is what I have:

        GameObject.Find("Main Camera").GetComponent<FrostEffect>().enabled = true;
        float duration = 2f;
        DOTween.To(x => FrostEffect.FrostAmount = x, 0.0f, 0.34f, duration)
            .OnComplete(()=>DOTween.To(x => FrostEffect.FrostAmount = x, 0.34f, 0.0f, duration));
        Time.timeScale = 0.05f;
        yield return new WaitForSeconds (6.5f);
        Time.timeScale = 1f;
share|improve this question
FrostEffect.valToBeLerped = Mathf.SmoothStep(0, 1f, FrostEffect.FrostAmount);
yield return new WaitForSeconds (1.01f);
FrostEffect.valToBeLerped = Mathf.SmoothStep(1, 0, FrostEffect.FrostAmount);

This will not start an animation of any sort unfortunately, what's happening here is you're setting the exact value to be FrostAmount, waiting 1.01 seconds and then setting it again. I understand what you're trying to do but it requires some sort of Tweening library.

Try DoTween for your project, it is a truly exceptional package in my opinion: http://dotween.demigiant.com/download.php

Once that's done, replace the above lines with:

float duration = 1.01f;
DOTween.To(x => FrostEffect.FrostAmount = x, 0.0f, 1.0f, duration)
.OnComplete(()=>DOTween.To(x => FrostEffect.FrostAmount = x, 1.0f, 0.0f, duration));

Make sure to include the using header for DoTween:

using DG.Tweening;

I haven't checked the above code 100% but I'll have another look when I get back from work. Best of luck!

share|improve this answer
    
Thank you it worked, I just have one question is there a way to just make (when the frost comes in) wait for a couple of seconds then use: .OnComplete(()=>DOTween.To(x => FrostEffect.FrostAmount = x, 1.0f, 0.0f, duration)); Thank you for your reply, it really helped!! :) – Jessca Stone 1 hour ago
    
Also is there a way not to make the script you gave me affected my time. timescale = to 0.01;. because the orb slows every thing down as well. so when the orb is done (using yield return new waitforseconds) then the script you gave me works. Thank you :) – Jessca Stone 1 hour ago
    
Please look above to see my second edit. Thank you – Jessca Stone 1 hour ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.