【问题标题】:How to get my shot delay to work如何让我的投篮延迟发挥作用
【发布时间】:2017-08-02 01:21:45
【问题描述】:

我试图通过 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);
             }
        }
    }
}

【问题讨论】:

  • 您能否发布视频的链接,并描述当前出了什么问题?比如,您是在暗示动画当前过快吗?
  • 嗯,视频不完全适合我的情况,但可以肯定。这就像 20 分钟,但他只是在前几分钟做计时器的事情,然后开始制造健康,这无关紧要。 youtube.com/watch?v=F6hUIU72JwE
  • 对不起,如果我不清楚。我只是想让一个计时器基本上工作。现在我没有得到结果。我可以使用没有问题的浮动来调整下一个箭头出来所需的时间。首先让代码工作是个问题。

标签: c# timer delay wait unity2d


【解决方案1】:

您的代码现在的结构方式是,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);
}

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

【讨论】:

  • 现在内心的喜悦……纯粹的……喜悦。谢谢老兄。
猜你喜欢
  • 2021-02-26
  • 1970-01-01
  • 2023-01-23
  • 1970-01-01
  • 2023-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-10
相关资源
最近更新 更多