[英]Start, pause and restart a timer in Unity C# - Only decrease timer inside trigger and reset when outside trigger
This is what I need it to do. 这就是我需要它做的事情。
Make a timer only decrease while staying in the trigger and reset when outside the trigger. 使得计时器仅在停留在触发器时减小并在触发器外部时重置。
When timer1 = 0, pause the countdown timer and set teleport to 0 当timer1 = 0时,暂停倒数计时器并将teleport设置为0
When timer1 = 1, Set the timer to 8 seconds and start counting down, IF timercountdown = 0, set teleport to 1 and set Timer1 to 0. (TO reset the timer) 当timer1 = 1时,将定时器设置为8秒并开始倒计时,IF timercountdown = 0,将teleport设置为1并将Timer1设置为0.(重置定时器)
This isnt working properly and Im really not sure why. 这不正常,我真的不知道为什么。
The timer gets locked at 8 and doesnt start decreasing when the Timer1 value equals 1. 当Timer1值等于1时,定时器锁定为8并且不会开始递减。
Thank you 谢谢
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ProximityTeleport : MonoBehaviour {
public float time;
public int timer1 = 0;
public int teleport = 0;
public float timecountdown;
void Start () {
}
void Update () {
if (timer1 == 0);
{
teleport = 0;
}
if (timer1 == 1);
{
timecountdown -= Time.deltaTime;
if (timecountdown <= 0.0f);
{
teleport = 1;
timer1 = 0;
}
}
}
void OnTriggerEnter() {
timecountdown = 8f;
timer1 = 1;
}
void OnTriggerExit() {
timer1 = 0;
}
}
I think it's some kind of logically problem. 我认为这是某种逻辑问题。 You say: 你说:
When timer1 = 0, pause the countdown timer and set teleport to 0 当timer1 = 0时,暂停倒数计时器并将teleport设置为0
but you do: 但你做了:
if (timer1 == 0);
{
timecountdown += Time.deltaTime;
teleport = 0;
}
And there is another problem: as long as timer1
= 1 you always set timecountdown = 8f;
还有另一个问题:只要timer1
= 1,你总是设置timecountdown = 8f;
here: 这里:
if (timer1 == 1);
{
timecountdown = 8f;
timecountdown -= Time.deltaTime;
if (timecountdown <= 0);
{
teleport = 1;
timer1 = 0;
}
}
Hope this helps! 希望这可以帮助!
Edit: according to your comments I think I know what you want to do: You only want to decrease while staying in the trigger? 编辑:根据你的评论,我想我知道你想做什么:你只想在触发时减少?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ProximityTeleport : MonoBehaviour {
public float time;
public int timer1 = 0;
public int teleport = 0;
public float timecountdown;
void Start () {
timecountdown = 8f;
}
void Update () {
if (timer1 == 0);
{
teleport = 0;
}
if (timer1 == 1);
{
timecountdown -= Time.deltaTime;
if (timecountdown <= 0.0f);
{
teleport = 1;
timer1 = 0;
timecountdown = 8f;
}
}
}
void OnTriggerEnter() {
//timecountdown = 8f;
timer1 = 1;
}
void OnTriggerExit() {
timer1 = 0;
}
}
I really want to help you but I don't know what you want to do exactly: 我真的很想帮助你,但我不知道你想要做什么:
You say: 你说:
Make a timer only decrease while staying in the trigger and reset when outside the trigger. 使得计时器仅在停留在触发器时减小并在触发器外部时重置。 When timer1 = 0, pause the countdown timer and set teleport to 0 When timer1 = 1, Set the timer to 8 seconds and start counting down, IF timercountdown = 0, set teleport to 1 and set Timer1 to 0. (TO reset the timer) 当timer1 = 0时,暂停倒计时器并将teleport设置为0当timer1 = 1时,将定时器设置为8秒并开始倒计时,IF timercountdown = 0,将teleport设置为1并将Timer1设置为0.(重置定时器)
What I understand: 我的理解:
On entering the trigger: 进入触发器时:
On leaving trigger: 离开时触发:
If countdowntimer is below zero: 如果倒计时器低于零:
Are there other rules for the teleport? 传送还有其他规则吗? You say if countdowntimer is zero you want to reset everthing. 你说如果countdowntimer为零你想重置everthing。 The teleport, too? 传送也是?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ProximityTeleport : MonoBehaviour {
public float time;
//public int timer1 = 0;
public bool triggerActive = false;
public int teleport = 0;
public float timecountdown;
void Start () {
timecountdown = 8.0f;
}
void Update () {
if (triggerActive)
{
timecountdown -= Time.deltaTime;
if (timecountdown <= 0.0f)
{
timecountdown = 8.0f;
teleport = 1;
// player has to re-enter the trigger:
triggerActive = false;
}
} else {
teleport = 0;
timecountdown = 8.0f;
}
}
void OnTriggerEnter() {
triggerActive = true;
}
void OnTriggerExit() {
triggerActive = false;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.