gpt4 book ai didi

ios - 如何添加计时器?

转载 作者:行者123 更新时间:2023-12-01 16:28:10 25 4
gpt4 key购买 nike

我有一个具有多点触摸功能的Unity脚本,它将检测触摸,查看触摸是否撞到了撞机(gameObject的一部分),然后检查它是否是正确的撞机,并销毁它。这是工作代码:

using UnityEngine.UI;
using UnityEngine;
using System.Collections;

public class GameController : MonoBehaviour

{

void Update () // Updates every frame
{
if (Input.touchCount != 0) // Triggered by a touch
{
foreach (Touch touch in Input.touches) // Triggered as many times as there are touches.
{
RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(touch.position), Vector2.zero);
if (hit && touch.phase == TouchPhase.Began && hit.collider.gameObject.tag ==("Fish")) // Triggered if touch hits something, when touch just begins, and if the object hit was tagged as a "Fish"
{
hit.collider.gameObject.GetComponent<FishScript>().TappedOut(); // Fish script activated
}
}
}
}
}

这是工作代码。现在,我想在其中添加一个计时器,并希望它可以计时触摸。我想做到这一点,只要玩家可以点击,然后将手指移到鱼上,它就会计数,只要在1秒钟内将手指移到鱼上即可。这是我编写的脚本,需要帮助:
using UnityEngine.UI;
using UnityEngine;
using System.Collections;

public class GameController : MonoBehaviour

{

void Update () // Updates every frame
{
if (Input.touchCount != 0) // Triggered by a touch
{
foreach (Touch touch in Input.touches) // Triggered as many times as there are touches.
{
if (touch.phase == TouchPhase.Began)
{
float touchTimer = Time.time;
int i = touch.fingerId;
}
RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(touch.position), Vector2.zero);
if (hit && hit.collider.gameObject.tag ==("Fish")) // Triggered if touch hits something, when touch just begins, and if the object hit was tagged as a "Fish"
{
hit.collider.gameObject.GetComponent<FishScript>().TappedOut(); // Fish script activated
}
}
}
}
}

到目前为止,这就是我所拥有的。我尝试使用return语句,但在Void Update()函数中不起作用。我尝试用while循环制作另一个函数,该循环要等到每一帧完成之后再经历另一个循环。那没有用。这个想法行得通吗?

最佳答案

这是Unity中的一个简单计时器。将CheckTimer()放在Update()方法上。

private double counter = 0.000d;
public double time = 2.000d; // How many seconds you want your timer to run

void CheckTimer()
{
counter+= Time.deltaTime;
if (counter >= time)
{
// Time has passed
counter = 0.000d;
// Do stuff here..
}
}

对于您的情况,可以在满足条件时将bool设置为true,然后进行设置,以便 CheckTimer()仅在条件为true时运行。

关于ios - 如何添加计时器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34031836/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com