这是我需要做的。
使计时器仅在停留在扳机中时减少,而在扳机外时重置。
当timer1 = 0时,暂停倒数计时器并将瞬移设置为0
当timer1 = 1时,将计时器设置为8秒并开始倒计时,如果 timercountdown = 0,将传送设置为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;
}
}
答案 0 :(得分:3)
我认为这是某种逻辑问题。你说:
当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;
}
}
希望这会有所帮助!
编辑:根据您的评论,我想我知道您想做什么: 您只想在扣动扳机时就减小幅度?
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;
}
}
答案 1 :(得分:2)
我真的很想帮助您,但我不知道您想做什么:
您说:
使计时器仅在保持触发状态时才减少,并在触发时复位 在触发器之外。当timer1 = 0时,暂停倒数计时器并 将传送设置为0当timer1 = 1时,将计时器设置为8秒,然后 开始递减计数,如果timercountdown = 0,将传送设置为1并设置 Timer1设为0。(以重置计时器)
我的理解:
输入触发器时:
离开触发器时:
如果倒数计时器小于零:
还有其他传送规则吗? 您说如果countdowntimer为零,则您想重置所有内容。传送也是吗?
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;
}
}