简体   繁体   中英

Simple resetting timer method to help lerp between a materials alpha values 1 - 0

I am working on a 'Shield' script (sci-fi, not RPG, imagine a spaceship) that only becomes active once a collision has happened. I'm trying to achieve this by having the alpha value of the material be set to 0f - then once 'hit' going straight up to 1f and then lerping back down to 0f. Rinse, repeat. However i'm having a problem coming up with the timer element of the lerp. The timers i've looked at tend to involve the update method or do not allow me to use it in the lerp. This is my code so far:

using UnityEngine;
using System.Collections;

public class Shield : MonoBehaviour {

public int shieldHealth;
private SpriteRenderer sRend;
private float alpha = 0f;

void Start () {
    shieldHealth = 10;
    sRend = GetComponentInChildren<SpriteRenderer>();
}

void OnCollisionEnter2D (Collision2D other) {
    shieldHealth --;
    LerpAlphaOfShield ();

    if(shieldHealth <= 0) {
        Destroy(this.gameObject);
    }
}

void LerpAlphaOfShield () {
    float lerp = Mathf.SmoothStep(1.0f, 0.0f, /*missing timer*/);

    alpha = Mathf.Lerp(0.0f, 1.0f, lerp);
    sRend.material.color = new Color(1, 1, 1, alpha);
}
}

Now i've thought that the SmoothStep part may be unnecessary once I have a timer, however this part I found in another question that was originally a PingPong that allowed the alpha values to go back and forth: I was trying to get it to do it just once then, obviously, reset with a timer.

Thanks in advance for any advice!

SOLUTION

I finally came up with this to reset and trigger again every hit and turn the alpha values of the shield from 1 - 0 in 1 second. If anyone thinks this code that I have could be improved please let me know, I would be very interested!

using UnityEngine;
using System.Collections;

public class Shield : MonoBehaviour {

public int shieldHealth;
private SpriteRenderer sRend;
private float alpha = 0f;
private bool hasCollided = false;
private float timer = 1f;

void Start () {
    shieldHealth = 10;
    sRend = GetComponentInChildren<SpriteRenderer>();
}

void Update () {

    timer -= Time.deltaTime;

    if(timer >= 0.001) {
        alpha = Mathf.Lerp(0.0f, 1.0f, timer);
        sRend.material.color = new Color(1, 1, 1, alpha);
    }

    if (timer <= 0) {
        hasCollided = false;
    }
}

void OnCollisionEnter2D (Collision2D other) {
    shieldHealth --;
    hasCollided = true;
    timer = 1f;

    if(shieldHealth <= 0) {
        Destroy(this.gameObject);
    }
}
}

A possible solution is to do this:

public float lerpSpeed = 5f; 
float currAlpha;
float goal = 0f;

void Update()
{
    currAlpha = Mathf.Lerp(currAlpha, goal, lerpSpeed * Time.deltaTime;
    sRend.material.color = new Color(1f, 1f, 1f, alpha);
    if(1f - currAlpha > .001f)
    {
         goal = 0f;
    }
}

And then in the OnCollisionEnter2D function set goal = 1f;

lerpSpeed is a variable you can play with to change how fast it lerps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM