Sign up ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm trying to make a relatively simple elevator (which essentially teleports the player), but I don't want it to instantly teleport them. I want the player to enter the collider, wait 2-3 seconds, and then do it to take make it seem somewhat more natural.

My code thus far:

using UnityEngine;
using System.Collections;

public class Teleporter : MonoBehaviour
{
    public GameObject TeleportTo;
    //public Material NewSkybox;

    void TimerInvoke()
    {

    }

    void OnTriggerEnter(Collider other)
    {

        Vector3 displacement = other.transform.position - this.transform.position;

        other.transform.position = TeleportTo.transform.position;
        other.transform.position += displacement;

        //RenderSettings.skybox = NewSkybox;
    }
}

Also if possible I'd like to stray away from coroutines. How should I do this?

share|improve this question
1  
Possible duplicate of How to load scene after a set delay? – Alexandre Vaillancourt 19 hours ago
1  
I'm curious as to why you would want to avoid the tool that is designed to cater to this exact scenario? – shadow 15 hours ago

4 Answers 4

You can also use Invoke to achieve.

void OnTriggerEnter(Collider other)
{
    Invoke("TeleportPlayer", 3f);
}
 void TeleportPlayer()
{

    Vector3 displacement = other.transform.position - this.transform.position;

    other.transform.position = TeleportTo.transform.position;
    other.transform.position += displacement;

    //RenderSettings.skybox = NewSkybox;
}
share|improve this answer

I usually do that with Coroutines, but since you were very specific that you want to avoid them, maybe in your case you could try something quite simple like:

float delay = 0;

public Update()
{
    if (delay > 0) delay -= Time.deltaTime;
    if (delay <= 0) TeleportPlayer();
}

void OnTriggerEnter(Collider other)
{
    delay = 3;
}

void TeleportPlayer()
{
    Vector3 displacement = other.transform.position - this.transform.position;
    other.transform.position = TeleportTo.transform.position;
    other.transform.position += displacement;
}

Of course, adjustments can be done depending on how much time you want to wait. Also, consider that deltaTime is not the same every frame, so if you need scientific-level precision for your waiting, some tweaking would probably be required. But I think you got the idea of what the code is doing.

Finally, maybe also of your interest, see this implementation of a WaitUntil function that was created precisely to avoid Coroutines: http://theinstructionlimit.com/a-replacement-for-coroutines

share|improve this answer
    
I tried your script, but I get "the name 'other' does not exist in the current context. – KernelPanic 15 hours ago
    
@KernelPanic Oh, the reason for that is all the "TeleportPlayer" function is just a piece of the OP question for the sake of illustrating the use of the code. Instead, you should just substitute it for whatever function you want to execute after the given delay. – MAnd 4 hours ago

Use Coroutines. You can see yield return new waitforseconds(3); in the TeleportPlayer method. That will wait 3 seconds before teleporting the player. Remember though, it only halts execution of the next line within the coroutine. All other methods continue to be executed.

 void OnTriggerEnter(Collider other)
        {
           StartCoRoutine("TeleportPlayer");

        }

IEnumerator TeleportPlayer()
{

            Vector3 displacement = other.transform.position - this.transform.position;
      Yield return new waitforseconds(3);

            other.transform.position = TeleportTo.transform.position;
            other.transform.position += displacement;

}
share|improve this answer
1  
Just noting that the OP mentioned that if possible, Coroutines should be avoided. Not that your answer is wrong or isn't useful. – MAnd 17 hours ago
    
@MAnd my bad didn't read that last line – SanSolo 17 hours ago

Of course, you can do it in code, but my approach is a little more "natural".

On your example - elevator - I will do something as follows:

  • When player enters elevator, play animation (fe. closing doors)
  • In this animation, at the end I would place code for "teleporting" player.

So you don't need to tweak everything by code, but you would do this in animation, so it's easier and more natural way.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.