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 fadeSpeed = 0.1f;
public bool toTeleport = false;
private List<Vector3> teleportersPositions = new List<Vector3>();
private bool teleported = false;
private Material material;
private GameObject myother;
private List<Component> components = new List<Component>();
// 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)
{
myother = other.gameObject;
material = objectToTeleportMaterial.GetComponent<Renderer>().material;
TeleportingVisualEffect(material, 0f, 5f);
}
// Update is called once per frame
void Update()
{
if(teleported == true)
{
myother.GetComponent<NavMeshAgent>().enabled = false;
myother.transform.position = teleporters[0].transform.position;
TeleportingVisualEffect(material, 1f, 5f);
teleported = false;
}
}
private void Teleport(GameObject objectToTeleport)
{
}
private void TeleportingVisualEffect(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 = false;
}
teleported = true;
}
private Material GetMaterial(GameObject ObjectToTeleport)
{
Material material = null;
if (ObjectToTeleport.GetComponent<Renderer>().material != null)
{
material = ObjectToTeleport.GetComponent<Renderer>().material;
}
else
{
foreach (Transform child in ObjectToTeleport.transform)
{
if (child.GetComponent<Renderer>().material != null)
{
material = child.GetComponent<Renderer>().material;
}
}
}
return material;
}
}
This code make an object to fade out/in by changing the alpha color.
The problem is in the function FadeTo when the variable targetOpacity value is 1 then in the Update the variable teleported is true all the time so it’s starting the coroutine over and over again and also the property NavMeshAgent is never enabled true again in the FadeTo :
if(targetOpacity == 1)
{
myother.GetComponent<NavMeshAgent>().enabled = false;
}
Now there are only two teleporters in the List and I can’t make a simple teleporting of the object/s between them. I want to teleport an object from teleporters[1] to teleporters[0] this flag teleported is not working as I thought it will.