1
\$\begingroup\$

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 ("Frost Effect by Steven Craeynest") on Unity Asset 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 attached 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;
\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

Hey sorry was sleeping this side of the world :)

Setting delay can be as simple as adding .SetDelay(float delay);

I imagine setting the timescale would be similar, as far as I know DoTween uses it's own timescale (DOTween.timeScale) so you could modify it to affect game timescale too.

You should spend some time in the DoTween docs and explore what's possible!

http://dotween.demigiant.com/documentation.php

But I've done it for you here this time, should work fine, hopefully haha.

float duration = 1.01f;
float delayBeforeUnfreeze = 3.0f;
float previousTimeScale = Time.timeScale;

//Slow timescale
Time.timeScale = 0.01f;

//Freeze time effect
DOTween.To(x =>FrostEffect.FrostAmount = x, 0.0f, 1.0f, duration)
  .OnComplete(()=>
  { 
    //Unfreeze tween effect
    DOTween.To(x => FrostEffect.FrostAmount = x, 1.0f, 0.0f, duration)
      .SetDelay(delayBeforeUnfreeze);

    //Set timescale back to previous.
    Time.timeScale = previousTimeScale;
  });

There are cleaner ways to do this via coroutine but that's another story, which I'm sure you will explore later on :)

Best of luck in your journey!

To mods: Sorry for another answer but it wouldn't fit in another comment. Let me know if theres anything else I can do to keep the answer clean. Cheers!

\$\endgroup\$
0
1
\$\begingroup\$
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!

\$\endgroup\$
7
  • \$\begingroup\$ 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!! :) \$\endgroup\$ Aug 22, 2016 at 6:59
  • \$\begingroup\$ 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 :) \$\endgroup\$ Aug 22, 2016 at 7:05
  • \$\begingroup\$ Please look above to see my second edit. Thank you \$\endgroup\$ Aug 22, 2016 at 7:14
  • \$\begingroup\$ I've added another answer to answer your other question, hope it helps :) \$\endgroup\$
    – quaternion
    Aug 22, 2016 at 21:32
  • 1
    \$\begingroup\$ Sorry nevermind, I fixed it. :) Thank you once again \$\endgroup\$ Aug 23, 2016 at 19:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .