繁体   English   中英

在Unity C#中启动,暂停和重新启动计时器 - 仅在触发器内减少计时器并在外部触发器时重置

[英]Start, pause and restart a timer in Unity C# - Only decrease timer inside trigger and reset when outside trigger

这就是我需要它做的事情。

使得计时器仅在停留在触发器时减小并在触发器外部时重置。

当timer1 = 0时,暂停倒数计时器并将teleport设置为0

当timer1 = 1时,将定时器设置为8秒并开始倒计时,IF timercountdown = 0,将teleport设置为1并将Timer1设置为0.(重置定时器)

这不正常,我真的不知道为什么。

当Timer1值等于1时,定时器锁定为8并且不会开始递减。

谢谢

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

我认为这是某种逻辑问题。 你说:

当timer1 = 0时,暂停倒数计时器并将teleport设置为0

但你做了:

    if (timer1 == 0);
    {
        timecountdown += Time.deltaTime;
        teleport = 0;
    }

还有另一个问题:只要timer1 = 1,你总是设置timecountdown = 8f; 这里:

    if (timer1 == 1);
    {
        timecountdown = 8f;
        timecountdown -= Time.deltaTime;

        if (timecountdown <= 0);
        {
            teleport = 1;
            timer1 = 0;
        }

    }

希望这可以帮助!

编辑:根据你的评论,我想我知道你想做什么:你只想在触发时减少?

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

我真的很想帮助你,但我不知道你想要做什么:

你说:

使得计时器仅在停留在触发器时减小并在触发器外部时重置。 当timer1 = 0时,暂停倒计时器并将teleport设置为0当timer1 = 1时,将定时器设置为8秒并开始倒计时,IF timercountdown = 0,将teleport设置为1并将Timer1设置为0.(重置定时器)

我的理解:

进入触发器时:

  • 开始或继续减少倒计时器
  • 在触发时减少倒计时器

离开时触发:

  • 暂停减少,但不要重置
  • 将传送设为0

如果倒计时器低于零:

  • 将传送设为1
  • 将countdowntimer重置为8秒
  • 继续减少?

传送还有其他规则吗? 你说如果countdowntimer为零你想重置everthing。 传送也是?

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM