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);
    }
}
}
Community
  • 1
  • 1
JonHerbert
  • 647
  • 1
  • 8
  • 23

1 Answers1

1

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.

Stas BZ
  • 1,184
  • 1
  • 17
  • 36
Colton White
  • 976
  • 5
  • 17
  • Thanks for the reply, however this doesn't seem to work, it's incredibly quick at low lerp speed and if i increase above to say 60, the alpha becomes 1 and won't reset. I'm looking for a lerp speed of about 1 to 3 seconds long. – JonHerbert May 27 '15 at 00:32