gpt4 book ai didi

c# - 在 Unity 中使用 Event Trigger SetGazedAt Lerping Color

转载 作者:太空宇宙 更新时间:2023-11-03 10:24:56 25 4
gpt4 key购买 nike

我正在使用 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);

}

关于c# - 在 Unity 中使用 Event Trigger SetGazedAt Lerping Color,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31956349/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com