【问题标题】:Unity2D Problem: Enemy does not shoot but it movesUnity2D问题:敌人不开枪但它移动
【发布时间】:2023-01-28 01:57:20
【问题描述】:

我在 Unity2D 上遇到敌人问题。 敌人应该跑向玩家,如果它在攻击范围内,那么它必须用子弹攻击敌人,但敌人会跟随我的玩家。 但有个问题: 敌人不会用子弹射击。 我更改了代码,所以当我按下按钮时,敌人也会攻击。 它仅适用于按钮。

这是我的敌人代码:

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

public class Enemy2 : MonoBehaviour
{

    public Transform target;
    public Transform firePoint;
    public float speed = 200f;
    public float nextWaypointDistance = 3f;

    public float range = 10f; // the range at which the enemy will start moving towards the player
    public float attackRange = 8f; // the range at which the enemy will attack the player
    float distance;
    public Transform enemyGFX;
    private Transform player; // reference to the player's transform
    public GameObject EnemyWeapon;

    Path path;
    int currentWaypoint = 0;
    bool reachedEndOfPath = false;

    Seeker seeker;
    Rigidbody2D rb;
    public Animator animator;
    bool Stop = false;
    public static float Enemy2Direction;

    // Start is called before the first frame update
    void Start()
    {
        seeker = GetComponent<Seeker>();
        rb = GetComponent<Rigidbody2D>();
        animator = GetComponentInChildren<Animator>();
        player = GameObject.FindGameObjectWithTag("Player").transform;


        InvokeRepeating("UpdatePath", 0f, .5f);
    }

    void UpdatePath()
    {
        if (seeker.IsDone())
        {
            seeker.StartPath(rb.position, target.position, OnPathComplete);
        }
    }

    void OnPathComplete (Path p)
    {
        if (!p.error)
        {
            path = p;
            currentWaypoint = 0;
        }
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        if (Pause.IsPause == false)
        {
            if (path == null)
            {
                return;
            }
            if (currentWaypoint >= path.vectorPath.Count)
            {
                reachedEndOfPath = true;
                return;
            }
            else
            {
                reachedEndOfPath = false;
            }

            Vector2 direction = ((Vector2)path.vectorPath[currentWaypoint] -rb.position).normalized;
            Vector2 force = direction * speed * Time.deltaTime;

            rb.AddForce(force);

            float distance = Vector2.Distance(rb.position, path.vectorPath[currentWaypoint]);

            if (distance < nextWaypointDistance)
            {
                currentWaypoint++;
            }
            //You can make look it differently, if you delete 'rb.velocity' and add 'force' instead.
            if (rb.velocity.x >= 0.01f)
            {
                enemyGFX.transform.localScale = new Vector3(-1f, 1f, 1f);
                Enemy2Direction = 1f;
            }
            else if (rb.velocity.x <= -0.01f)
            {
                enemyGFX.transform.localScale = new Vector3(1f, 1f, 1f);
                Enemy2Direction = 0f;
            }

            if (distance < attackRange)
            {
                if (Stop = false)
                {
                    StartCoroutine(Attack());
                }
            }
        }

        distance = Vector2.Distance(transform.position, player.position);
        if (distance > range)
        {
            Destroy(gameObject);
        }
        
    }

    void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "Player")
        {
            animator.SetBool("Damage", true);
        }
        if (collision.gameObject.tag == "Bullet")
        {
            animator.SetBool("Damage", true);
        }
    }
    void Update()
    {
        if (animator.GetBool("Damage"))
        {
            StartCoroutine(DamageAnimation());
        }
        if (Input.GetKeyDown(KeyCode.L))
    {
        StartCoroutine(Attack());
    }
    }

    IEnumerator Attack()
    {
        Debug.Log("Attacked");
        Stop = true;
        yield return new WaitForSeconds(2.0f);
        animator.SetBool("Attack", true);
        Instantiate(EnemyWeapon, firePoint.position, firePoint.rotation);
        yield return new WaitForSeconds(2.0f);
        animator.SetBool("Attack", false);
        StartCoroutine(Wait());
    }

    IEnumerator DamageAnimation()
    {
        yield return new WaitForSeconds(2.0f);
        animator.SetBool("Damage", false);
    }

    IEnumerator Wait()
    {
        yield return new WaitForSeconds(2.0f);
        Stop = false;
    }
}

我不知道我的代码有什么问题。 有人能帮帮我吗?

【问题讨论】:

  • 因为if (Stop = false) 总是将 stop 设置为 false?

标签: c# unity3d


【解决方案1】:
if(Stop == false)

用==检查Stop bool时替换=,在C#中使用==来检查是否相等

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-14
    • 2022-11-16
    • 1970-01-01
    • 2015-02-16
    • 2022-01-10
    • 2023-03-21
    • 1970-01-01
    • 2023-02-08
    相关资源
    最近更新 更多