Search Unity

FPS hold position for X time -> LoadLevel(NextLevel).

Discussion in 'Scripting' started by Santiagogarciavelez, Jun 20, 2018.

  1. Santiagogarciavelez

    Santiagogarciavelez

    Joined:
    Jul 14, 2015
    Posts:
    2
    Hi everyone,

    I'm kind of bad and new on coding. For now, i'm only working with easy functions like OnTrigger.

    I would like prog something and i don't know how to start, so if someone can help me i'll be very pleased.

    I hope this thread is on the right forum and the question hasn't been asked yet.

    So the event i want to prog is this:


    - If the FPS prefab hold the same position (x,y,z) for Time.timeSinceLevelLoad + 90f (approximatly 1.5 min right?)

    {Application.LoadLevel( FirstLevel); // it will respawn the FPS on the first level.
    }

    - If the FPS prefab move everything is false and the FPS continue on the level.

    I don't know how to use Coroutine, or Transform function yet.

    Thanks any way.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    This is definitely a great place to learn ALL of the above items: Learn
     
  3. Santiagogarciavelez

    Santiagogarciavelez

    Joined:
    Jul 14, 2015
    Posts:
    2
    Ok so I manage to solved it by my self.

    thanks any way. Hope can be usefull other noobs like me.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6. public class FrozePlayerRespawn : MonoBehaviour
    7. {
    8.  
    9.     public GameObject Player;
    10.     Vector3 lastposition;
    11.     Vector3 currentPosition;
    12.     float timePlayerStop;
    13.     float timetoNextLevel = 20;
    14.     float frozeTimeElapsed;
    15.  
    16.  
    17.  
    18.     void Update()
    19.     {
    20.  
    21.         lastposition = Player.transform.position;
    22.         timePlayerStop = Time.timeSinceLevelLoad;
    23.         StartCoroutine(frozeTimeRespawn());
    24.  
    25.     }
    26.  
    27.     IEnumerator frozeTimeRespawn()
    28.     {
    29.  
    30.         yield return new WaitForSeconds(20);
    31.         currentPosition = Player.transform.position;
    32.         frozeTimeElapsed = Time.timeSinceLevelLoad;
    33.  
    34.         if (currentPosition == lastposition && timePlayerStop + timetoNextLevel == frozeTimeElapsed) ;
    35.         {
    36.  
    37.             Application.LoadLevel(0);
    38.  
    39.         }
    40.  
    41.     }
    42.  
    43.  
    44. }
    45.