我正在使用 cardboard sdk,我正在尝试使用适用于 Unity 的 cardboard sdk 对注视对象的颜色进行 lerp。我正在使用下面的 lerping 脚本,当我将它附加到对象时它独立于注视运行,但是当我尝试使它的一个版本在 SetGazedAt 中工作时它不起作用。
下面是用于更改对象颜色的 SetGazedAt 代码。我将如何替换它或调用我拥有的名为 fill 的 Lerp 脚本?
填充.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 方法
public void SetGazedAt(bool gazedAt) {
GetComponent<Renderer>().material.color = gazedAt ? Color.clear : Color.red;
}
我在@piojo 的帮助下弄明白了。我使用协程来处理 lerping 更新并删除了填充脚本。我现在只是在较大的传送脚本中调用 FillObject 函数。
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;
}
}
这是 lerp 函数
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);
}
我是一名优秀的程序员,十分优秀!