0
\$\begingroup\$

In general the main goal is to teleport between teleporters with fade out/in when teleporting. but for testing the Fade I call the Fade method once in the Start :

using System;
using System.Collections;
using System.Collections.Generic;
using System.Net.WebSockets;
using UnityEngine;
using UnityEngine.Scripting.APIUpdating;

public class Teleporting : MonoBehaviour
{
    public List<ObjectToTeleport> objectsToTeleport;
    public List<ObjectToTeleport> currentlyTeleportedObjects;
    public List<GameObject> teleporters;
    public Transform objectToFade;
    public GameObject[] groups;
    public bool loop = false;
    public bool random = false;

    public int teleportationsCount = 0;

    [Serializable]
    public class ObjectToTeleport
    {
        public GameObject teleportableObject;
        public int teleportOrder;
    }

    public void Start()
    {
        if (teleporters.Count > 1 && objectsToTeleport.Count > 0)
        {
            InvokeRepeating("MoveTeleportableObjects", 3, 3f);
        }

        FadeTo(objectToFade.GetComponent<Renderer>().material, 0, 3);
    }

    private void MoveTeleportableObjects()
    {
        if (teleportationsCount < objectsToTeleport.Count)
            currentlyTeleportedObjects.Add(objectsToTeleport[teleportationsCount]);

        for (int i = 0; i < currentlyTeleportedObjects.Count; i++)
        {
            if (!loop)
            {
                MoveObjects(i);
            }
            else
            {
                MoveObjects(i);
            }
        }
        teleportationsCount++;
    }

    private void MoveObjects(int i)
    {           
        GameObject destinationTeleporter = teleporters[currentlyTeleportedObjects[i].teleportOrder];
        currentlyTeleportedObjects[i].teleportableObject.transform.position = destinationTeleporter.transform.position;
        if (currentlyTeleportedObjects[i].teleportOrder < teleporters.Count - 1)
        {
            currentlyTeleportedObjects[i].teleportOrder++;
        }
        else if (loop == true)
        {
            {
                currentlyTeleportedObjects[i].teleportOrder = 0;
            }
        }
    }

    IEnumerator FadeTo(Material material, float targetOpacity, float duration)
    {
        Color color = material.color;
        float startOpacity = color.a;

        float t = 0;

        while (t < duration)
        {
            t += Time.deltaTime;
            float blend = Mathf.Clamp01(t / duration);

            color.a = Mathf.Lerp(startOpacity, targetOpacity, blend);

            material.color = color;

            yield return null;
        }
    }
}

The objectToFade is a simple 3d cube but it's not fading out. and when I try to select the cube material to change it to transparent the cube material is Default-Material and when I click on it it's not selecting it in the Assets like it's not exist.

  1. How to fade the cube and any other objects out and then to fade it in and with option bool flag to fade out/in automatic nonostop ?

  2. How to fade in out when teleporting ? Fade out teleport then fade in in the teleported teleporter.

Screenshot of the project and cube with the default-material :

Teleporting

\$\endgroup\$
3
  • \$\begingroup\$ Did you consider creating a new material to use for your fade-out effect? The default is just a default. If you want something that's not exactly like the default, then making your own asset is step 1. \$\endgroup\$
    – DMGregory
    Jan 8 at 20:48
  • \$\begingroup\$ @DMGregory created a new material and added it to the cube but now I noticed that either in the editor if I change the alpha color from 255 to 0 or by the script the cube is not fading to the end there. the cube is transparent but you can still see it. ah changing to Fade in the material is doing the job. \$\endgroup\$ Jan 8 at 21:01
  • 1
    \$\begingroup\$ Want to post your solution as an answer? \$\endgroup\$
    – DMGregory
    Jan 8 at 21:05

0

You must log in to answer this question.

Browse other questions tagged .