Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Help with timer

Discussion in 'Getting Started' started by Krista4468, Apr 24, 2024.

  1. Krista4468

    Krista4468

    Joined:
    Aug 17, 2023
    Posts:
    2
    I am extremely new to Unity. I want the player to hit an object and then be teleported to the beginning after a few seconds. I got the teleport to work but not the timer. Can anyone help me get the timer to work? Here is the code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class FinishBackToStart : MonoBehaviour
    6. {
    7.     public float teleportBack;
    8.  
    9.     private float timer;
    10.  
    11.     private Status status = Status.Idle;
    12.  
    13.     // Update is called once per frame
    14.     void Update()
    15.     {
    16.         if (status == Status.Activated)
    17.         {
    18.             if (UpdateTimer())
    19.             {
    20.                     status = Status.Recovering;
    21.                     timer = teleportBack;
    22.             }
    23.         }
    24.         else if (status == Status.Recovering)
    25.         {
    26.             if (UpdateTimer())          
    27.                 {
    28.                     status = Status.Idle;
    29.                 }          
    30.         }
    31.     }
    32.     public void Activate()
    33.     {
    34.         if (status != Status.Idle)
    35.         {
    36.             TeleportPlayer script = gameObject.GetComponent<TeleportPlayer>();
    37.             if (script != null)
    38.             {
    39.                 script.TeleportPlayerToPosition();
    40.                 return;
    41.             }
    42.  
    43.         }        timer = teleportBack;
    44.         status = Status.Activated;
    45.     }
    46.     public bool UpdateTimer()
    47.     {
    48.         if (timer > 0)
    49.         {
    50.             timer -= Time.deltaTime;
    51.  
    52.             if (timer <= 0)
    53.                 {
    54.                     timer = 0;
    55.                     return true;
    56.                 }          
    57.         }
    58.         return false;
    59.     }
    60.     private enum Status
    61.     {
    62.         Idle,
    63.         Activated,
    64.         Recovering
    65.     }
    66. }
     
  2. BrianLH369

    BrianLH369

    Joined:
    Feb 26, 2022
    Posts:
    10
    If the only thing you need to do is teleport after hitting an object you can use Invoke(). Something like this maybe..
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class FinishBackToStart : MonoBehaviour
    4. {
    5.     public float teleportBack = 2.0f;//lets say delay is 2 seconds
    6.  
    7.     public void ObjectHit()//call this method when you hit an object
    8.     {
    9.         //here lets say you destroy the object or change its position or whatever
    10.  
    11.         //and after that you can use invoke to delay a method like this
    12.         Invoke("Teleport", teleportBack);//Teleport method will be called in 2 seconds
    13.     }
    14.     public void Teleport()
    15.     {
    16.         TeleportPlayer script = gameObject.GetComponent<TeleportPlayer>();
    17.         if (script != null)
    18.         {
    19.             script.TeleportPlayerToPosition();
    20.         }
    21.     }
    22. }
     
  3. Krista4468

    Krista4468

    Joined:
    Aug 17, 2023
    Posts:
    2
    It worked, thank you! :)