如何让我的拍摄延迟工作

How to get my shot delay to work

我正在尝试学习如何使用 youtube 上的 gamesplusjames 执行此操作,但效果不是很好。我很确定这是小事,但我不知道它是什么。基本上,当我释放 C 键时,我的角色瞄准她的弓箭并射击。但我需要延迟,这样她的射击速度就不会超过她的动画移动速度。谁能告诉我我把球丢在哪里了?我简化了我的代码以摆脱与射击或瞄准无关的所有其他垃圾。谢谢

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

public class tulMove : MonoBehaviour {

public Transform arrowSpawn;
public GameObject arrowPrefab;

private bool aim = false;
private bool shot = false;

public float shotDelay;
private float shotDelayCounter;

private Rigidbody2D rb;
private Animator anim;

void Start ()
{

    anim = GetComponent<Animator>();
    rb = GetComponent<Rigidbody2D>();

}

void Update(){

    if (!aim && Input.GetKeyDown (KeyCode.C))
    {
        aim = true;
        anim.SetTrigger ("aim");
    }

    if (aim && !shot && Input.GetKeyUp (KeyCode.C)) 
    {

        shot = true;
        aim = false;
        anim.SetTrigger ("shot");
        Instantiate (arrowPrefab, arrowSpawn.position, arrowSpawn.rotation);
        shotDelayCounter = shotDelay;
    }

    if (aim && !shot && Input.GetKeyUp (KeyCode.C)) 
    {
        shotDelayCounter -= Time.deltaTime;

        if (shotDelayCounter <= 0) 
        {
            shotDelayCounter = shotDelay;
            shot = true;
            aim = false;
            anim.SetTrigger ("shot");
            Instantiate (arrowPrefab, arrowSpawn.position, arrowSpawn.rotation);
             }
        }
    }
}

您的代码现在的结构方式是,shotDelayCounter 仅在其 if 语句为真时才会被调用,但看起来并非一直如此。将 shotDelayCounter -= Time.deltaTime; 移到 if 语句之外,以便在每一帧中调用它。类似于:

shotDelayCounter -= Time.deltaTime;

if (aim && !shot && Input.GetKeyUp (KeyCode.C) && shotDelayCounter <= 0) 
{
    shotDelayCounter = shotDelay;
    shot = true;
    aim = false;
    anim.SetTrigger ("shot");
    Instantiate (arrowPrefab, arrowSpawn.position, arrowSpawn.rotation);
}

现在您的计数器应该可以正常工作了,因为它总是会被调用