【问题标题】:Is there a way to teleport player after countdown timer hits zero? on unity倒计时归零后有没有办法传送玩家?关于团结
【发布时间】:2020-08-02 18:39:47
【问题描述】:

我对脚本和统一也很陌生,在我的场景中,我希望玩家在倒数计时器达到零后从当前位置传送到某个位置,有没有办法做到这一点?我在网上搜索,但是我找不到很多关于它的提示,所以我在这里问。

我做了一个我在网上找到的基本代码计时器

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

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    你有很多方法可以做“传送”,但它基本上是改变物体变换的位置,所以如果你想让物体到达 3D 空间位置(0,1,0),只需将其分配给它:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-05
      • 1970-01-01
      • 1970-01-01
      • 2022-12-20
      • 2020-12-18
      • 1970-01-01
      • 2020-12-18
      • 1970-01-01
      相关资源
      最近更新 更多