Looking for a Fade in/out Script for the teleport script i am using

Hi! I am looking for a fade in/out or something like a delay for my 2way teleport, preventing my player from popping into the scene again. Here is the script i’m using, my problem is i’m a total scripting noob so i need your help.

using UnityEngine;

public class Teleporter : MonoBehaviour
{

    public bool teleported = false;
    public Teleporter destination;

    void OnTriggerEnter(Collider c)
    {

        if (c.CompareTag("Player"))
        {

            if (!teleported)
            {
                destination.teleported = true;
                c.gameObject.transform.position = destination.gameObject.transform.position;
            }
        }

    }

    void OnTriggerExit(Collider c)
    {

        if (c.CompareTag("Player"))
        {

            teleported = false;
        }

    }

}

One way you could do it is to have a fullscreen GUITexture, which you then manipulated the alpha on. For example:

using UnityEngine;

public class Teleporter : MonoBehaviour
{

    public bool teleported = false;

    public Teleporter destination;
    //Caching the transform for better performance. It's best to always do this.
    public Transform _transform;

    

    //Create a GameObject in your scene and set it's position to 0.5,0.5,0, then add a GUItexture and add a texture to it.
    //Set the color to whatever you want the teleport color to be, and the alpha will be handled by this script.
    public GUITexture fadeTexture;

    //Lerping speed for the fade out.
    public float fadeInSpeed;

    //This defines the offset for the teleported oject to be at when teleporting.
    public Vector3 offset;

    public AudioClip soundEffect;

    void Start()
    {
        _transform = transform;
    }

    void Update()
    {
        //Get the original color, modify it's alpha, then send it back to fadeTexture.
        Color oldColor = fadeTexture.color;

        oldColor.a = Mathf.Lerp(oldColor.a, 0, fadeInSpeed);

        fadeTexture.color = oldColor;
    }

    void OnTriggerEnter(Collider c)
    {
        if (c.CompareTag("Player"))
        {
            if (!teleported)
            {
                destination.teleported = true;
                
                Color oldColor = fadeTexture.color;
                audio.PlayOneShot(soundEffect);
                //Setting the alpha to 1 causes a flash effect.
                oldColor.a = 1;
                fadeTexture.color = oldColor;
                c.gameObject.transform.position = destination._transform.position + offset;
                
            }

        }
    }



    void OnTriggerExit(Collider c)
    {

        if (c.CompareTag("Player"))
        {
            teleported = false;

        }
    }

    
}

This works great for me, and looks pretty good. With the right fadeInSpeed, it looks very much like TF2’s teleporter. Oh, I went ahead and put in a simple sound effect thing and a teleport offset. It was putting the new Sample FPS Character in the ground, so it seems necessary :smile:

I hope I was helpful!

Perfect!

Ok now i only have the problem when i hit play everything works, i have only the gui when i enter the teleporter but when i build an run my scene, the only thing i see is the black gui screen.

Hmm, that’s very strange, I just built my scene and it works fine… Perhaps you could try setting the GUITexture’s alpha to zero in edit mode? That would stop the nice fade in at the start, but it would make you be able to see your game for sure.

When you say create a gameObject, do i create an empty one? I just created a GuiTexture which has auto set it’s position to 0,5 0,5 0? Sry i’m quite a noob in unity :wink:

Both ways work, I’m just personally in the habit of creating objects from empties. :slight_smile: Anyway, so long as the scale is all ones, and the position is 0.5, 0.5, 0, it should work… try putting this code in the start method of a script attached to the GUITexture, and then setting the GUITexture’s color’s “a” to 0:

 // replace Color.white with your own color.
guiTexture.color = Color.white;

Ok setting alpha to zero did the trick. But now when i build and run the Teleport doesn’t work debug says NullReferenceException: object reference not set to an instance of an object. But only when i build the scene in playmode no errors :face_with_spiral_eyes:

Ok finally :smile: the problem was i had no audio effect, that’s why i got the error message!

THX for your help !!!