使用WaitForSecond和鼠标单击时无法获得良好的结果 [英] Not getting the good result when using WaitForSecond and Mouse click

查看:120
本文介绍了使用WaitForSecond和鼠标单击时无法获得良好的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个块放置脚本,该脚本每0.15秒放置一个块,并且它运行完美.问题是,当我尝试执行相同的操作来破坏块时,而不是每0.3秒破坏一次块,而是等待0.3秒然后每帧破坏一个块.我该如何解决?

I'm trying to do a block placing script that place a block every 0.15 second, and it work perfectly. The problem is when I try to do the same thing to destroy blocks, instead of destroying a block every 0.3 second, it wait 0.3 second then destroy one block per frame. How can I fix this?

我尝试将其放在其他脚本中,但似乎也不起作用.奇怪的是,如果我在其中添加Debug.Log("1")...:

I tried putting it in an other script but it doesn't seem to work either. Weird thing is that if I add Debug.Log("1") inside...:

void Place(){
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    RaycastHit hitInfo;
    if(Physics.Raycast(ray, out hitInfo)){
        //here

...每帧打印"1".但是,如果我将Debug.Log("1")放在if if (hitInfo.collider == up){之一中,则每0.3秒打印一次"1".对不起,我的英语/解释不好

...it prints "1" every frame. But if i put Debug.Log("1") inside one of the if if (hitInfo.collider == up){ it prints "1" every 0.3 second. Sorry for my bad english/explanation

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class placeBlock : MonoBehaviour {
    public GameObject cube;
    public Collider up;
    public Collider bottom;
    public Collider right;
    public Collider left;
    public Collider front;
    public Collider back;

    void Update() {
        if(Input.GetMouseButton(0)) {
             StartCoroutine(MyMethod());
        }
        if(Input.GetMouseButton(1)) {
             StartCoroutine(MyMethod2());
        }
    }

    void Place2() {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hitInfo;
        if(Physics.Raycast(ray, out hitInfo)) {
             Destroy(hitInfo.transform.parent.gameObject);
        }
    }

    void Place() {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hitInfo;
        if(Physics.Raycast(ray, out hitInfo)) {

        if (hitInfo.collider == up) {
                Instantiate(cube,new Vector3(this.transform.position.x,this.transform.position.y + 1,this.transform.position.z), Quaternion.identity);
            }
            if (hitInfo.collider == bottom) {
                Instantiate(cube,new Vector3(this.transform.position.x,this.transform.position.y - 1,this.transform.position.z), Quaternion.identity);
            }
            if (hitInfo.collider == right) {
                Instantiate(cube,new Vector3(this.transform.position.x - 1,this.transform.position.y,this.transform.position.z), Quaternion.identity);
            }
            if (hitInfo.collider == left) {
                Instantiate(cube,new Vector3(this.transform.position.x + 1,this.transform.position.y,this.transform.position.z), Quaternion.identity);
            }
            if (hitInfo.collider == front) {
                Instantiate(cube,new Vector3(this.transform.position.x,this.transform.position.y,this.transform.position.z - 1), Quaternion.identity);}
            if (hitInfo.collider == back){
                Instantiate(cube,new Vector3(this.transform.position.x,this.transform.position.y,this.transform.position.z + 1), Quaternion.identity);
            }
        }
    } 

    IEnumerator MyMethod(){
        yield return new WaitForSeconds(0.15F);
        Place();
    }

    IEnumerator MyMethod2(){
        yield return new WaitForSeconds(0.3F);
        Place2();
    }
}

推荐答案

我注意到的第一件事是您在每个帧处都调用它.

The first thing I note is that you're calling this at each frame.

if(Input.GetMouseButton(0)) {
             StartCoroutine(MyMethod());
        }

这意味着您将在每一帧(按下鼠标时)继续启动协程.我建议将那个方法移到Update Loop之外,或尝试添加一些布尔值标志以避免在每一帧调用它.

This mean you will keep starting the coroutine at each frame (when you press the mouse). I would suggest to move that method outside of the Update Loop, or try to add some boolean flag to avoid calling it at each frame.

您将在更新中看到类似的内容.

You will have something like this in update.

void Update()
{
        //you require a bool flag for each element
        if(Input.GetMouseButton(0) && !_alreadyPressed) {
             _alreadyPressed = true;
             StartCoroutine(MyMethod());
        }
}

然后将协程中的布尔值重置.这样.

And then you reset the bool in the coroutine. Like this.

IEnumerator MyMethod()
{
       //you could do something here

       yield return new WaitForSeconds(0.15F);

       //do something else
       _alreadyPressed = false;
}

这篇关于使用WaitForSecond和鼠标单击时无法获得良好的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆