简体   繁体   English

倒计时归零后有没有办法传送玩家? 关于团结

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

I'm very new to scripting and to unity as well, In my scene, I would like the player from its current location to teleport to a certain location after the countdown timer hits zero, is there a way to do this?我对脚本和统一也很陌生,在我的场景中,我希望玩家在倒数计时器达到零后从当前位置传送到某个位置,有没有办法做到这一点? I research online however I could not find many tips about it thus I am asking here.我在网上研究,但是我找不到很多关于它的提示,所以我在这里问。

I did a basic code timer I found online我做了一个我在网上找到的基本代码计时器

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: 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);

For the timer you can use Invoke or InvokeRepeating methods or a countdown like yours.对于计时器,您可以使用InvokeInvokeRepeating方法或像您这样的倒计时。

So in your code it will looke something like:所以在你的代码中它看起来像:

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