我有一个带有多点触控功能的Unity脚本,可以检测触摸,看看触摸是否碰到了对手(游戏对象的一部分),然后检查它是否是正确的对撞机,并将其摧毁。这是工作代码:
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循环创建另一个函数,该循环将等到每个帧完成后再通过另一个循环。那没用。这个想法会起作用吗?
答案 0 :(得分:0)
这是Unity中的一个简单计时器。将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..
}
}
方法上。
CheckTimer()
在您的情况下,您可以在满足条件时将bool设置为true,然后使其socket.on('connect', function () {
port.postMessage('connect');
});
socket.on('disconnect', function () {
port.postMessage('disconnect');
});
仅在条件为真时运行。
答案 1 :(得分:0)
要解决此问题,请通过在顶部实例化并按以下步骤操作,使计时器成为全局变量
using UnityEngine.UI;
using UnityEngine;
using System.Collections;
public class GameController : MonoBehaviour
{
public float timer;
public float duration;
void Start(){
duration = 1; //you can set this in code or in your hierarchy in Unity
}
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)
{
timer = Time.time + duration;
}
if(Time.time < timer){
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
}
}
}
}
}
}
我没有其余的代码来确保这可行,但其背后的逻辑确实如此。您希望将计时器设置为当前时间加上给定的持续时间。如果当前时间小于你在玩家做某事时设定的计时器,那么你知道他们在到达计时器之前就做了他们的动作。这有意义吗?
答案 2 :(得分:0)
我知道我并不想通过说谢谢来占据评论空间,但感谢Jonnus。现在唯一让我感到困惑的是我怎么没有早点想到这一点。无论如何,这里是任何需要多点触控脚本帮助的人的代码。
using UnityEngine.UI;
using UnityEngine;
using System.Collections;
Private float timer; // Control variable
Public float duration; // How long you want your touch to be valid
Void Start ()
{
duration = 1; // Also adjustable in the interface
}
Void Update ()
{
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)
{
timer = Time.time + duration;
}
RaycastHit2D objectPlayerTouched = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(touch.position), Vector2.zero);
// var objectPlayerTouched is the 2DCollider for whatever the player tapped on.
if (objectPlayerTouched != null && Time.time < timer) // Triggered if player taps an object and time is still valid
{
objectPlayerTouched(destroy); // This means whatever object the player tapped is now destroyed
}
}
}
}
再一次,非常感谢Jonnus。我认为这会复杂得多。另外,不要做我做的事情,忘记在界面中设置持续时间,最后花10分钟寻找问题......相信我这很烦人。