在 Unity 中使用 Event Trigger SetGazedAt Lerping Color
Lerping Color using Event Trigger SetGazedAt in Unity
我正在使用 cardboard sdk,并且我正在尝试使用适用于 Unity 的 cardboard sdk 对注视对象的颜色进行 lerp。我正在使用下面的 lerping 脚本,当我将它附加到对象时它独立于凝视运行,但是当我尝试使它的一个版本在 SetGazedAt[=25 中工作时它不起作用=].
下面是 SetGazedAt 的代码,它改变了对象的颜色。我将如何替换它或调用我拥有的 Lerp 脚本,我将其命名为 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 方法
public void SetGazedAt(bool gazedAt) {
GetComponent<Renderer>().material.color = gazedAt ? Color.clear : Color.red;
}
将 bool gazedAt 设置为静态并在填充 class 中使用它,方法是指定你在其中声明 gazedAt 的 class if(classNAME.gazedAt == true){lerp代码在这里}; .还要确保填充脚本附加到场景中的游戏对象。
我了解到您实际上并不需要 Fill
class,但您只想修改 SetGazedAt
以获取百分比或时间,而不是布尔值 (插值而不是颜色 on/off).
既然 lerp 没有那么复杂,我建议你丢掉另一个 class。您将需要 start/end/current 次,并且需要开始值和结束值。 lerp 函数将 start/end 颜色作为第一个参数,将百分比(作为 [0,1]
范围内的分数)作为第三个参数。分数是(current-start)/(end-start)
(当变化完成一半时,这是0.5,或者50%的颜色A,50%的颜色B。)
所以SetGazedAt
可以很简单:
GetComponent<Renderer>().material.color = Color.Lerp(Color.clear, Color.red, fraction);
当然,你需要时间信息作为参数传入,这样你才能计算分数。另请注意,分数限制为 [0,1]
,因此您无需担心超出范围。
我在@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);
}
我正在使用 cardboard sdk,并且我正在尝试使用适用于 Unity 的 cardboard sdk 对注视对象的颜色进行 lerp。我正在使用下面的 lerping 脚本,当我将它附加到对象时它独立于凝视运行,但是当我尝试使它的一个版本在 SetGazedAt[=25 中工作时它不起作用=].
下面是 SetGazedAt 的代码,它改变了对象的颜色。我将如何替换它或调用我拥有的 Lerp 脚本,我将其命名为 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 方法
public void SetGazedAt(bool gazedAt) {
GetComponent<Renderer>().material.color = gazedAt ? Color.clear : Color.red;
}
将 bool gazedAt 设置为静态并在填充 class 中使用它,方法是指定你在其中声明 gazedAt 的 class if(classNAME.gazedAt == true){lerp代码在这里}; .还要确保填充脚本附加到场景中的游戏对象。
我了解到您实际上并不需要 Fill
class,但您只想修改 SetGazedAt
以获取百分比或时间,而不是布尔值 (插值而不是颜色 on/off).
既然 lerp 没有那么复杂,我建议你丢掉另一个 class。您将需要 start/end/current 次,并且需要开始值和结束值。 lerp 函数将 start/end 颜色作为第一个参数,将百分比(作为 [0,1]
范围内的分数)作为第三个参数。分数是(current-start)/(end-start)
(当变化完成一半时,这是0.5,或者50%的颜色A,50%的颜色B。)
所以SetGazedAt
可以很简单:
GetComponent<Renderer>().material.color = Color.Lerp(Color.clear, Color.red, fraction);
当然,你需要时间信息作为参数传入,这样你才能计算分数。另请注意,分数限制为 [0,1]
,因此您无需担心超出范围。
我在@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);
}