C# 有没有办法在倒数计时器归零后传送玩家?论统一
我对脚本和unity都很陌生,在我的场景中,我希望玩家在倒数计时器达到零后从当前位置传送到某个位置,有没有办法做到这一点?我在网上研究,但是我找不到很多关于它的提示,所以我在这里问 我做了一个我在网上找到的基本代码计时器C# 有没有办法在倒数计时器归零后传送玩家?论统一,c#,unity3d,C#,Unity3d,我对脚本和unity都很陌生,在我的场景中,我希望玩家在倒数计时器达到零后从当前位置传送到某个位置,有没有办法做到这一点?我在网上研究,但是我找不到很多关于它的提示,所以我在这里问 我做了一个我在网上找到的基本代码计时器 using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class Timer : MonoBehaviour {
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);
}
}
“传送”的方法有很多,但基本上是改变对象变换位置,因此,如果希望对象进入3D空间位置
(0,1,0)
,只需将其指定给它:
this.transform.position = new Vector3(0,1,0);
对于计时器,您可以使用或方法或类似您的倒计时
因此,在您的代码中,它看起来像:
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);
}
}