为什么当将对象 alpha 颜色更改为 0 时,对象会闪烁一毫秒?



一般来说,传送是有效的,但在第一个传送器上,当alpha在物体消失前的最后变为0时,物体闪烁一毫秒,你看到一毫秒后,整个物体再次消失并移动到第二个传送器。

这个想法是,当将 alpha 更改为 0 时它会消失,并在第二个传送器中变回 1。不知道为什么它在更改为 0 结束时闪烁。

我现在再次检查,出于某种原因,它在更新中执行了两次代码,而不是一次。即使出于某种原因将传送设置为 false,它也会连续两次执行所有操作。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class Teleporting : MonoBehaviour
{
public List<GameObject> teleporters = new List<GameObject>();
public GameObject objectToTeleportMaterial;
public float fadeDuration = 5;
public float fadeInTargetOpacity = 0;
public float fadeOutTargetOpacity = 1;
private List<Vector3> teleportersPositions = new List<Vector3>();
private bool teleported = false;
private Material material;
private GameObject myother;

// Start is called before the first frame update
void Start()
{
teleporters.AddRange(GameObject.FindGameObjectsWithTag("Teleporter"));
if (teleporters.Count > 0)
{
foreach (GameObject teleporter in teleporters)
{
teleportersPositions.Add(teleporter.transform.position);
}
}
}
private void OnTriggerEnter(Collider other)
{
teleported = false;
myother = other.gameObject;
material = objectToTeleportMaterial.GetComponent<Renderer>().material;
Teleport(material, fadeInTargetOpacity, fadeDuration);
}
// Update is called once per frame
void Update()
{
if (teleported == true)
{
myother.GetComponent<NavMeshAgent>().enabled = false;
myother.GetComponent<AgentControl>().enabled = false;
myother.transform.position = teleporters[0].transform.position;
Teleport(material, fadeOutTargetOpacity, fadeDuration);
teleported = false;
}
}
private void Teleport(Material material, float fadeTargetOpacity, float fadeDuration)
{
MaterialExtensions.ToFadeMode(material);
StartCoroutine(FadeTo(material, fadeTargetOpacity, fadeDuration));
}
// Define an enumerator to perform our fading.
// Pass it the material to fade, the opacity to fade to (0 = transparent, 1 = opaque),
// and the number of seconds to fade over.
IEnumerator FadeTo(Material material, float targetOpacity, float duration)
{
// Cache the current color of the material, and its initiql opacity.
Color color = material.color;
float startOpacity = color.a;
// Track how many seconds we've been fading.
float t = 0;
while (t < duration)
{
// Step the fade forward one frame.
t += Time.deltaTime;
// Turn the time into an interpolation factor between 0 and 1.
float blend = Mathf.Clamp01(t / duration);
// Blend to the corresponding opacity between start & target.
color.a = Mathf.Lerp(startOpacity, targetOpacity, blend);
// Apply the resulting color to the material.
material.color = color;
// Wait one frame, and repeat.
yield return null;
}
if (targetOpacity == 1)
{
myother.GetComponent<NavMeshAgent>().enabled = true;
myother.GetComponent<AgentControl>().enabled = true;
}
if (targetOpacity == 0)
{
teleported = true;
}
}
}

尝试使用 DOTween 将 alpha 补间为 0 或目标值。 也许 Clamp 毁了你的阿尔法。 您所需要的只是从资源商店安装 DOTween,使用

using DG.Tweening;

并用material.DOFade(*targetAlpha*, *durationOfTween*);补间阿尔法

最新更新