Precise timing for teleporting Player several times

I need to teleport the Player in the rythym of a song or at any time of the song. For example after 10 seconds, then 4.5 seconds later and then 13.2 secs later, always to an other place of the map.

I guess my code is quite poor and it doesnt really work as the Player is stuck from 10 to 12 seconds at the desired point and jumps back to his original position afterwards - also it is just one “Timestamp” and it is not very accurate:

 public class TimeTransport : MonoBehaviour
 {
     public float delayBeforeLoading = 10f;
     public float delayAfterLoading = 12f;
 
     private float timeElapsed;
 
     public GameObject thePlayer;
     public Transform teleportTarget;
 
 
 
     void Update()
     {
         timeElapsed += Time.deltaTime;
 
         if (timeElapsed > delayBeforeLoading && timeElapsed < delayAfterLoading)
         {
             thePlayer.transform.position = teleportTarget.transform.position;
         }
     }
 }

I would really appreciate some help :slight_smile:
Rian

What is this line of code mean? Like if the player presses the button between those times?

if (timeElapsed > delayBeforeLoading && timeElapsed < delayAfterLoading)

I ask because the way I see it now, it will teleport the player automatically after 10 seconds and not stop until after 12. You might just need to input an if statement like this:

if (timeElapsed > delayBeforeLoading && timeElapsed < delayAfterLoading)
          {
		if(Input.GetKeyDown(KeyCode.Space)
		{
              	thePlayer.transform.position = teleportTarget.transform.position;
          	}
	}

However if you are trying to figure out why your character is acting weird, it’s because you automatically have them set to teleoport between 10 and 12 seconds with that statement I put on top.

Thanks for your answer @skatesa207 !

The line means that there is a timewindow where the Player is at a specific place (it was just for testing).

No there should be no Button pressed. It is just a timing thing. The Player should be teleported to a place and is free to move until i want him to be teleported to the next place and so on.


To be exact the questions are:

  • how can i teleport him at a very specific time? And after some time teleport him to the next place.

  • How can i make him free to move?


I guess i can make him move again with a boolean?!

But most important is to let him jump from place to place at a specific time in the song. For example at 12.5 sec then at 22.2 sec and 43.2 sec of the song - always a differnt place (i thought about switching scenes but i guess the loading time would mess it all up).

You can try something like this:

 if(times.Count != 0))
 {
     if(timeElapsed >= timeYouWantToMove)
     {
         // Debug
         Debug.Log("Reached " + times[0]);

         // Reached Time, remove current time from list
         times.RemoveAt(0);

         // Set Next Timer
         if(times.Count > 0)
        {
             Debug.Log("Setting new Time: " + times[0]);
             timeYouWantToMove = times[0];
             timeElapsed = 0;
         }
     } 
     else timeElapsed += Time.deltaTime;
 }
 else Debug.Log("Cycle Complete, no times remain in loop");

At any time you can include a function to add more times:

public void AddTime(float time)
{
    times.Add(time);
}

@ Rian_Itch && everyone else: Just use a Coroutine. Like so:

   Class TeleportPlayer : MonoBehaviour 
    {
    public WaitForSeconds ten, thirteen_and_half, etc; 
    public Coroutine timer;
     public void Start()
    {
     ten = new WaitForSeconds(10f);
    thirteen_and_half = new WaitForSeconds(13.5f);
    etc = new WaitForSeconds(31337f);
    }
    
    public void StartTeleport(WaitForSeconds param) //Give this function an argument that was initialized in Start() at ANY point in time to achieve desired functionality.
    {
     if(timer == null) //This check acts to prevent multiple calls to the coroutine below while enabling exactly one call to exist at any time.
    {
    timer = StartCoroutine(this.TeleportOnTime(param));
    }
    }
    //Private so that outside classes don't accindentally try to call it instead (nothing will happen, tough to debug!
    private void TeleportOnTime(WaitForSeconds param)
    {
    yield return param; //This will wait the number of seconds given to it (ie. params time)
    
    player.Teleport(); //Do your thing
    
    timer = null; //Reset to null to enable another call
    }
    }