簡體   English   中英

Unity 2D,弓箭射擊實例化問題

[英]Unity 2D, Shooting with bow Instantiate problem

大家好,我的弓箭射擊腳本有點問題。 我的意思是,播放動畫的最后一幀時,我想射箭。 我嘗試通過設置一個FirePoint GameObject來做到這一點,並通過在所需幀中的“動畫”選項卡中進行錄制來放置它。 它當然是禁用的,但是在播放動畫時啟用,然后再次禁用。 所以問題是:-當我按下與我的“射擊”輸入相匹配的按鈕時,動畫就會播放,-我的實例化出現並產生多個箭頭,-禁用它時,它停止產生箭頭。

我只想產生一個箭頭。 有人可以幫忙嗎?


CombatScript.cs:

/*    private bool shootBow;
 *    public bool needReload = false;
 *    public float reloadTime = 1.5f;
 *    public float realoadCD;
 */
public void RangeAttack()
{
    if (needReload == false && gameObject.GetComponent<PlayerControls>().grounded == true && Input.GetButtonDown("Ranged"))
    {
        animator.SetTrigger("shootBow");

        attack1 = false; // Melle attacks sets to false in case of lag or smth.
        attack2 = false;
        attack3 = false;



        needReload = true;

        if (needReload == true)
        {
            reloadCD = reloadTime;
        }
    }

    if (reloadCD > 0 && needReload == true)
    {
        reloadCD -= Time.deltaTime;
    }

    if (reloadCD <= 0)
    {
        reloadCD = 0;
        needReload = false;
    }
    if (firePoint.gameObject.activeSelf == true)
    {
        Instantiate(Missile, new Vector3(firePoint.position.x + 1, firePoint.position.y), firePoint.rotation);
        Debug.Log("It's a bird, a plane, no.. it's arrow.");
    }
}

箭頭Controller.cs:

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

public class ArrowController : MonoBehaviour {

    public float speed;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update ()
    {
        GetComponent<Rigidbody2D>().velocity = new Vector2(speed, GetComponent<Rigidbody2D>().velocity.y);
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.tag == "Enemy")
        {
            Destroy(collision.gameObject);
        }

        Debug.Log("Arrow Broke");
        Debug.Log(gameObject.name);
        //Destroy(gameObject);
    }

    public void OnCollisionEnter2D(Collision2D collision)
    {

    }
}

我的情況示例:

我的情況的例子

正確/錯誤NeedReload語句的示例:

true / false NeedReload語句的示例

在右邊的檢查器中,您有我的玩家信息,在左邊(或底部)的檢查器中有導彈(箭頭)檢查器

您可以使用動畫事件 ,例如布爾值就是您的射擊率。

您的代碼可能看起來像這樣:

float LastShoot;
        float FireRate = 10;
        public void OnShoot()
        {
            if (LastShoot <= 0)
            {
                //Shoot your arrow
                LastShoot = FireRate;
            }
        }
        void Update()
        {
            LastShoot -= 0.5f;
        }

但是我不知道這是否是計算射速的最佳解決方案。 如果有人知道更好的話,請編輯我的遮陽篷:)

Okey ...幾個小時后(我浪費了...)。 答案很簡單。 對於諸如此類的未來“問題”……事情是在我的腳本函數中創建“ ProduceArrow()”,然后(除了我所做的)在Animation Tab中創建Animation Event之類的東西,當您創建動畫時間軸時,需要單擊鼠標右鍵來調用它,然后選擇它並選擇適當的功能。

一些反饋-gif

您的代碼現在工作的方式,每幀都會調用Instatiate。 因此,一鍵單擊(可能長於一幀)將觸發多個箭頭,因此您需要為何時調用實例化設置條件。 您已經計算出重新加載時間,而這正是您所需要的。 您只需要將它包含在if語句中,如下所示:

if (firePoint.gameObject.activeSelf == true && !needReload)
{
    Instantiate(Missile, new Vector3(firePoint.position.x + 1, firePoint.position.y), firePoint.rotation);
    Debug.Log("It's a bird, a plane, no.. it's arrow.");
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM