【问题标题】:trying to make an enemy shoot a projectile at the player when the player enters the enemys range当玩家进入敌人射程时,试图让敌人向玩家发射弹丸
【发布时间】:2022-04-17 00:21:38
【问题描述】:

由于某种原因,弹丸会被发射,但仅在敌人与玩家接触时才会发射,并且由于某种原因非常缓慢。 下面是我的代码。

(我的弹丸上有一个单独的脚本,但只处理对玩家的伤害)

public class flyingEnemy : MonoBehaviour {


    public int maxHealth = 40;
    Rigidbody2D rb2d;

    public float speed;



    public float attackRange;
    private float lastAttackTime;
    public float attackDelay;

    public Transform target;
    public float chaseRange;

    public GameObject projectile;
    public float bulletForce;
    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        float distanceToTarget = Vector3.Distance(transform.position, target.position);
        if(distanceToTarget < chaseRange)
        {
            //start chasing player
            Vector3 targetDir = target.position - transform.position;
            float angle = Mathf.Atan2(targetDir.y, targetDir.x) * Mathf.Rad2Deg - 90f;
            Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);
            transform.rotation = Quaternion.RotateTowards(transform.rotation, q, 180);

            transform.Translate(Vector3.up * Time.deltaTime * speed);

        }
        if(distanceToTarget < attackRange)
        {

            //check to see if its time to attack again
            if (Time.time > lastAttackTime + attackDelay)
            {
                //do we have lineofsight?
                RaycastHit2D Hit = Physics2D.Raycast(transform.position, transform.up,attackRange);
                //what did the raycast hit?
                if (Hit.transform == target)
                {
                    GameObject newBullet = Instantiate(projectile, transform.position, transform.rotation);
                    newBullet.GetComponent<Rigidbody2D>().AddRelativeForce(new Vector2(0f, bulletForce));
                    lastAttackTime = Time.time;
                }
            }
        }


    }

【问题讨论】:

  • 好吧,弹丸似乎只在玩家与敌人接触时才会发射,我希望它们可以发射任何距离,但它们也很慢。

标签: c# unity3d


【解决方案1】:

我修改了您的代码,现在似乎可以使用了。我向玩家游戏对象添加了一个“玩家”层,然后将您的脚本重写为以下内容:

using UnityEngine;

public class Enemy : MonoBehaviour
{

public int maxHealth = 40;

public float speed;
public float attackRange;
public float attackDelay;
public float chaseRange;
public float bulletForce;
private float lastAttackTime;
private float distanceToTarget;

public Transform target;
public GameObject projectile;
private Rigidbody2D rb2d;

private void Start()
{
    rb2d = GetComponent<Rigidbody2D>();
}

private void FixedUpdate()
{
    if (distanceToTarget < chaseRange)
    {
        Chase(target.position);
    }
    else
    {
        rb2d.velocity = Vector2.zero;
    }
}

private void Update()
{
    distanceToTarget = Vector3.Distance(transform.position, target.position);

    if (distanceToTarget < attackRange)
    {
        if (Time.time > lastAttackTime + attackDelay)
        {
            RaycastHit2D Hit = Physics2D.Raycast(transform.position, transform.up, attackRange, 1 << LayerMask.NameToLayer("Player"));
            if (Hit)
            {
                Fire();
                lastAttackTime = Time.time;
            }
        }
    }
}

private void Chase(Vector3 target)
{
    Vector3 targetDir = target - transform.position;
    float angle = Mathf.Atan2(targetDir.y, targetDir.x) * Mathf.Rad2Deg - 90f;
    Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);
    transform.rotation = Quaternion.RotateTowards(transform.rotation, q, 180);
    rb2d.velocity = targetDir.normalized * speed;
}

private void Fire()
{
    GameObject newBullet = Instantiate(projectile, transform.position, transform.rotation);
    newBullet.GetComponent<Rigidbody2D>().AddForce(transform.up * bulletForce, ForceMode2D.Impulse);
}    
}

首先,如果您有一个刚体,请使用它而不是 Transform.Translate。其次,确保您的光线投射仅适用于 Player 层。 第三,而不是

AddRelativeForce(new Vector2(0f, bulletForce));

使用

AddForce(transform.up * bulletForce, ForceMode2D.Impulse);

第四,使用序列化的值,直到得到你想要的结果。我所做的是降低敌人的速度并增加子弹威力。如果您有任何问题,请告诉我。

【讨论】:

  • 非常感谢您的帮助,我对如何使用刚体而不是 transform.translate 函数感到困惑?
  • 我在代码里给你写好了。它在“Chase”函数中:rb2d.velocity = targetDir.normalized * speed;
  • 你帮了很大的忙,它现在按预期工作非常感谢:)
猜你喜欢
  • 1970-01-01
  • 2021-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-11
  • 1970-01-01
相关资源
最近更新 更多