2

I'm using the cardboard sdk and I'm trying to lerp the color of an object when it is gazed at using the cardboard sdk for Unity. I'm using the lerping script below and it functions independently of the gaze when I attach it to the object, but it doesn't work when I try to make a version of it work inside of SetGazedAt.

Below is the code for SetGazedAt that changes a color of the object. How would I replace this or call the Lerp script that I have, which I have titled fill?

Fill.cs

using UnityEngine;
using System.Collections;

public class Fill : MonoBehaviour {

float lerpTime = 2f;
float currentLerpTime;

//float moveDistance = 5f;



Color startColor = Color.clear;
Color endColor = Color.clear;



public void Start() {
    startColor = Color.clear;
    //endColor = new Color(0.0f,0.0f,1.0f,0.5f);
}

public void Update() {



    //reset when we press spacebar
    if (Input.GetKeyDown(KeyCode.Space)) {
        currentLerpTime = 0f;
        startColor = Color.clear;
        endColor = new Color(0.0f,0.0f,1.0f,0.5f);
    }

    //increment timer once per frame
    currentLerpTime += Time.deltaTime;
    if (currentLerpTime > lerpTime) {
        currentLerpTime = lerpTime;
    }

    //lerp!
    float perc = currentLerpTime / lerpTime;
    GetComponent<Renderer>().material.color = Color.Lerp(startColor, endColor, perc);
}

}

SetGazedAt Method in Teleport script

public void SetGazedAt(bool gazedAt) { 


    GetComponent<Renderer>().material.color = gazedAt ? Color.clear : Color.red;


}
AddWeb Solution Pvt Ltd
  • 21,025
  • 5
  • 26
  • 57
user808996
  • 179
  • 4
  • 15

3 Answers3

2

I have figured it out with help from @piojo. I used Coroutines to take care of the lerping update and dropped the fill script. I'm now just calling the FillObject function in my larger teleport script.

public void SetGazedAt(bool gazedAt) { 
        if (gazedAt == true) {
            aniTrigger = true;
            startColor = Color.clear;
            endColor = new Color(0.0f,0.0f,1.0f,0.8f);
            StartCoroutine(FillObject (startColor,endColor, 3 ));
            //GetComponent<Renderer>().material.color = Color.blue;
        } else {
            GetComponent<Renderer>().material.color = Color.clear;
        }


    }

and this for the lerp function

IEnumerator FillObject(Color startColor, Color endColor, float overTime)
    {
        float startTime = Time.time;
        while(Time.time < startTime + overTime)
        {
            //transform.position = Vector3.Lerp(source, target, (Time.time - startTime)/overTime);
            GetComponent<Renderer>().material.color = Color.Lerp(startColor, endColor, (Time.time - startTime)/overTime);
            yield return null;
        }
        GetComponent<Renderer> ().material.color = endColor;
        var x = this.gameObject.name;
        TeleportNow (x);

    }
user808996
  • 179
  • 4
  • 15
0

set the bool gazedAt as static and use it in your fill class by specifying the class in which you declared gazedAt like this if(classNAME.gazedAt == true){lerp code goes here}; . Also make sure the fill script is attached to gameobject in the scene.

LumbusterTick
  • 1,067
  • 10
  • 21
0

I gather that you don't actually need the Fill class, but you just want to modify SetGazedAt to take a percent or a time, instead of a bool (interpolate instead of color on/off).

Since lerp isn't that complicated, I suggest you lose the other class. You'll need start/end/current times, and you'll need the start and end value. The lerp function takes the start/end colors as the first parameters, and the percent (as a fraction in the [0,1] range) as the third. The fraction is (current-start)/(end-start) (This is 0.5 for when the change is half completed, or 50% color A, 50% color B.)

So SetGazedAt can be very simple:

GetComponent<Renderer>().material.color = Color.Lerp(Color.clear, Color.red, fraction);

Of course, you need timing information passed in as parameters, so you can calculate the fraction. Note also that the fraction is clamped to [0,1], so you don't need to worry about going out of bounds.

piojo
  • 6,351
  • 1
  • 26
  • 36
  • I had to keep the bool, but I changed it to an if else loop which works. But how do I countdown the Lerp inside of this function? In my fill script I had an update function that took care of the time. – user808996 Aug 12 '15 at 16:43
  • I'm using float perc = currentLerpTime / lerpTime; GetComponent().material.color = Color.Lerp(startColor, endColor, perc); – user808996 Aug 12 '15 at 16:46
  • 1
    @user808996 Thanks for the correction about parameters. As far as time, are you calling this function just once, and you want the color to animate for a moment afterwards? That's normally called a `tween`, and there are a bunch of libraries to help with that. But to do it yourself, you need to start a coroutine, which will act similar to the update function. Coroutines are a big topic, too narrow for this margin to contain. – piojo Aug 12 '15 at 17:09
  • @user808996 And if you intend to call it more than once (perhaps from another class's `Update` function), either the caller or the `Teleport` class will need to keep track of the time. There's no sexy technique (no equivalent to c++ function-`static` variables), you just need to store the values in the class. Oh, and you probably know this, but the field to get the current time (in seconds) is `Time.time`. – piojo Aug 12 '15 at 17:11
  • Thanks for the help. I posted the answer that ended up working. – user808996 Aug 13 '15 at 20:45