【问题标题】:Start, pause and restart a timer in Unity C# - Only decrease timer inside trigger and reset when outside trigger在 Unity C# 中启动、暂停和重新启动计时器 - 仅在触发器内部减少计时器并在外部触发器时重置
【发布时间】:2018-06-22 22:06:42
【问题描述】:

这是我需要它做的。

让计时器只在停留在触发器中时减少,并在触发器之外时重置。

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

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

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

计时器锁定在 8 点,当 Timer1 的值等于 1 时不会开始减少。

谢谢

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

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

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

    当 timer1 = 0 时,暂停倒数计时器并将瞬移设置为 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;
            }
    
        }
    

    希望这会有所帮助!

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

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

    【讨论】:

    • 我认为“timecountdown += Time.deltaTime;”暂停了我的 deltatime 倒计时
    • iv将timecountdown = 8f的位置从if语句改为void OnTriggerExit()
    • 好的,您的修改看起来不错。您可以删除timecountdown += Time.deltaTime; 并将if (timecountdown &lt;= 0); 更改为if (timecountdown &lt;= 0.0f);
    • 定时器锁定在8,当Timer1等于1时它不会减少
    • 虽然timer1 == 1 你的timecountdown 应该减少... timer1 是否保持1 的值?还是在 OnTriggerEnter 之后立即调用 OnTriggerExit ?或许您可以在这些函数中添加一些Debug.Log() 并共享日志?
    【解决方案2】:

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

    你说:

    让计时器仅在停留在触发器中时减少,并在以下情况下重置 在触发器之外。当 timer1 = 0 时,暂停倒数计时器并 将瞬移设置为 0 当 timer1 = 1 时,将计时器设置为 8 秒,然后 开始倒计时,如果 timercountdown = 0,将 Teleport 设置为 1 并设置 Timer1 为 0。(重置定时器)

    我的理解:

    进入触发器时:

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

    离开触发器时:

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

    如果倒计时小于零:

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

    传送还有其他规则吗? 你说如果倒数计时器为零,你想重置一切。传送也是?

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

    【讨论】:

    • 离开触发器时:重置计时器
    • 当它达到 0 yep 时重置一切
    • 好的,我将代码更改为完全重置。玩家必须重新进入触发器才能开始新的倒计时。
    • if (triggerActive);删除;
    • 当触发激活为真时,传送立即激活,而不是等待 8 秒,似乎它没有开始 time.delta 倒计时。 TImecountdown 卡在 8 秒,但瞬移仍然变成 1
    猜你喜欢
    • 2020-06-05
    • 2023-01-31
    • 2021-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-21
    • 1970-01-01
    相关资源
    最近更新 更多