簡體   English   中英

倒計時歸零后有沒有辦法傳送玩家? 關於團結

[英]Is there a way to teleport player after countdown timer hits zero? on unity

我對腳本和統一也很陌生,在我的場景中,我希望玩家在倒數計時器達到零后從當前位置傳送到某個位置,有沒有辦法做到這一點? 我在網上研究,但是我找不到很多關於它的提示,所以我在這里問。

我做了一個我在網上找到的基本代碼計時器

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Timer : MonoBehaviour
{
    public float timeRemaining = 150;
    public bool timerIsRunning = false;
    public Text timeText;

    private void Start()
    {
        // Starts the timer automatically
        timerIsRunning = true;
    }

    void Update()
    {
        if (timerIsRunning)
        {
            if (timeRemaining > 0)
            {
                timeRemaining -= Time.deltaTime;
                DisplayTime(timeRemaining);
            }
            else
            {
                Debug.Log("Time has run out!");
                timeRemaining = 0;
                timerIsRunning = false;
            }
        }
    }

    void DisplayTime(float timeToDisplay)
    {
        timeToDisplay += 1;

        float minutes = Mathf.FloorToInt(timeToDisplay / 60);
        float seconds = Mathf.FloorToInt(timeToDisplay % 60);

        timeText.text = string.Format("{0:00}:{1:00}", minutes, seconds);
    }
}

You have many ways to do the "teleport", but it's basically change object transform position, so if you want that the object goes to the 3D space position (0,1,0) , just assign it to it:

this.transform.position = new Vector3(0,1,0);

對於計時器,您可以使用InvokeInvokeRepeating方法或像您這樣的倒計時。

所以在你的代碼中它看起來像:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Timer : MonoBehaviour
{
///
    public GameObject objectToTeleport = null; //assign it from inspector or code
    public Vector3 destination = new Vector3(0,0,0); //assign it from inspector or code
///
    public float timeRemaining = 150;
    public bool timerIsRunning = false;
    public Text timeText;

    private void Start()
    {
        // Starts the timer automatically
        timerIsRunning = true;
    }

    void Update()
    {
        if (timerIsRunning)
        {
            if (timeRemaining > 0)
            {
                timeRemaining -= Time.deltaTime;
                DisplayTime(timeRemaining);
            }
            else
            {
                Debug.Log("Time has run out!");
                timeRemaining = 0;
                timerIsRunning = false;
                //Move object
                objectToTeleport.transform.position = destination;
            }
        }
    }

    void DisplayTime(float timeToDisplay)
    {
        timeToDisplay += 1;

        float minutes = Mathf.FloorToInt(timeToDisplay / 60);
        float seconds = Mathf.FloorToInt(timeToDisplay % 60);

        timeText.text = string.Format("{0:00}:{1:00}", minutes, seconds);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM