Post by eldv on Apr 25, 2021 22:21:56 GMT
Hi All,
Not exactly an Invector specific script, but I thought I would share my simple Teleporter script with the community. The teleporter will require at least one trigger collider/object and one additional empty transform to work. The script is attached to the Trigger object, and the empty transform is the destination of the teleport. If you are using a portal type system like the video example you, will need two trigger colliders and two empty receiver transforms for each portal to work as shown. They can be created to teleport one way, two way, or multiple daisy chained teleports.
I have an example of the script working on my YouTube Channel.
Not exactly an Invector specific script, but I thought I would share my simple Teleporter script with the community. The teleporter will require at least one trigger collider/object and one additional empty transform to work. The script is attached to the Trigger object, and the empty transform is the destination of the teleport. If you are using a portal type system like the video example you, will need two trigger colliders and two empty receiver transforms for each portal to work as shown. They can be created to teleport one way, two way, or multiple daisy chained teleports.
I have an example of the script working on my YouTube Channel.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Teleport : MonoBehaviour
{
[Header("Teleporter Tag and Receiver Target Options:")]
[Tooltip("This is the transform of the Receiver target. This is where the subject will reappear. Recommended Configuration is Front Trigger -> Rear Receiver, Rear Trigger -> Front Receiver.")]
public Transform target;
[Tooltip("The Unity tag which will activate the Teleporter. Will usually be 'Player'.")]
public string tagFilter;
private GameObject player;
[SerializeField]
[Tooltip("Wait time in seconds before teleportation occurs.")]
[Range(0.0f, 10.0f)] float wait;
[Tooltip("The Teleporter Sound Effect.")]
public AudioClip teleportSFX;
[Header("Teleportation Particle and Sound FX Options:")]
[Tooltip("Particle Effect that instantiates when Teleporter is activated.")]
public GameObject effect;
[Tooltip("Adjust the X-Y-Z offset from Local 0 that the effect will appear.")]
public Vector3 offset;
private AudioSource audiosource;
private AudioSource VFXAudiosource;
[Tooltip("The Sound Effect if any for the Teleport VFX.")]
public AudioClip VFXSoundFX;
[SerializeField]
[Tooltip("Sound Effects Volume.")]
[Range(0.0f, 1.0f)] private float vol;
void OnTriggerEnter(Collider other)
{
if (other.tag == tagFilter)
{
player = other.gameObject;
audiosource = gameObject.AddComponent<AudioSource>();
VFXAudiosource = gameObject.AddComponent<AudioSource>();
var othertrans = other.transform;
StartCoroutine(pauseOnTeleport(othertrans, target, player));
}
}
IEnumerator pauseOnTeleport(Transform othertrans, Transform target, GameObject player)
{
yield return new WaitForSeconds(wait);
if (teleportSFX != null)
{
audiosource.clip = teleportSFX;
audiosource.volume = vol;
audiosource.Play();
}
Instantiate(effect, othertrans.position + offset, Quaternion.identity);
if (VFXSoundFX != null)
{
VFXAudiosource.clip = VFXSoundFX;
VFXAudiosource.volume = vol;
VFXAudiosource.Play();
}
yield return new WaitForSeconds(.1f);
player.transform.position = target.transform.position;
Instantiate(effect, othertrans.position + offset, Quaternion.identity);
if (VFXSoundFX != null)
{
VFXAudiosource.clip = VFXSoundFX;
VFXAudiosource.volume = vol;
VFXAudiosource.Play();
yield return new WaitForSeconds(VFXSoundFX.length);
Destroy(VFXAudiosource);
Destroy(audiosource);
}
}
}
Instructions for Creating Teleport Prefab: