Side Scroller KnockBack

问题描述 投票:0回答:0

我正在用 C# 在 Unity 上做一个横向滚动。这是一个脚本,允许玩家在击中敌人时造成挫折。

此脚本在地图上只有一个敌人时有效。但是当有 2 个时,脚本就不再起作用了。当我点击第二个敌人时,第一个将在 2 上受到 1 次挫折。 脚本在玩家攻击脚本上

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

public class PlayerAttack : MonoBehaviour
{

    public PlayerMovement playerMovement;


    public Transform attackpointDroite;
    public Transform attackpointGauche;
    public float attackRange = 0.5f;
    public LayerMask enemyLayers;
    public GameObject NounoursDroite;
    public GameObject NounoursGauche;
    public bool nounoursDroite = false;
    public bool nounoursGauche = false;

    public GameObject AttackPointDroite;
    public GameObject AttackPointGauche;

    public bool AttackDroite = false;
    public bool AttackGauche = false;

    public float attackRate = 2f;
    float nextAttackTime = 0f;

    private Rigidbody2D rb;

    public Enemy enemy;
    public bool KBEKnockFromRight;

    public Transform playerTransform;

    public SpriteRenderer spriteRenderer;


    // Update is called once per frame
    void Update()
    {
        

        

        if (Time.time >= nextAttackTime)
        {
            if (Input.GetKeyDown(KeyCode.JoystickButton2) && spriteRenderer.flipX == false)
            {
                AttackDroite = true;
                attackdroite();
                StartCoroutine(ActiveNounoursDroite());
                nextAttackTime = Time.time + 1f / attackRate;
            }
            else if (Input.GetKeyDown(KeyCode.JoystickButton2) && spriteRenderer.flipX == true)
            {
                AttackGauche = true;
                attackgauche();
                StartCoroutine(ActiveNounoursGauche());
                nextAttackTime = Time.time + 1f / attackRate;
            }
        }
    }

    void attackdroite()
    {
        Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(attackpointDroite.position, attackRange, enemyLayers);

        foreach (Collider2D hitEnemy in hitEnemies)
        {
            if (hitEnemy.gameObject.CompareTag("Enemy"))
            {
                enemy.KBECounter = enemy.KBETotalTime;
                if (hitEnemy.transform.position.x <= transform.position.x)
                {
                    enemy = hitEnemy.GetComponent<Enemy>();
                    enemy.KBEKnockFromRight = false;
                }

                Debug.Log("Tah La droite" + hitEnemy.name);
                hitEnemy.GetComponent<Enemy>().TakeDamage(1);
            }
        }
    }

    void attackgauche()
    {
        Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(attackpointGauche.position, attackRange, enemyLayers);

        foreach (Collider2D hitEnemy in hitEnemies)
        {
            if (hitEnemy.gameObject.CompareTag("Enemy"))
            {
                enemy.KBECounter = enemy.KBETotalTime;
                if (hitEnemy.transform.position.x <= transform.position.x)
                {
                    enemy = hitEnemy.GetComponent<Enemy>();
                    enemy.KBEKnockFromRight = true;
                }

                Debug.Log("Tah La gauche" + hitEnemy.name);
                hitEnemy.GetComponent<Enemy>().TakeDamage(1);
            }
        }
    }

    private void OnDrawGizmosSelected()
    {
        if (attackpointDroite == null)
            return;

        Gizmos.DrawWireSphere(attackpointDroite.position, attackRange);
    }

    IEnumerator ActiveNounoursDroite()
    {
        NounoursDroite.SetActive(true);
        nounoursDroite = true;

        if (nounoursDroite == true)
        {
            yield return new WaitForSeconds(0.20f);
            NounoursDroite.SetActive(false);
            nounoursDroite = false;
        }
    }

    IEnumerator ActiveNounoursGauche()
    {
        NounoursGauche.SetActive(true);
        nounoursGauche = true;

        if (NounoursGauche == true)
        {
            yield return new WaitForSeconds(0.20f);
            NounoursGauche.SetActive(false);
            nounoursGauche = false;
        }
    }
}

我希望当我击中敌人时,他会击退`

c# unity3d
© www.soinside.com 2019 - 2024. All rights reserved.