Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 敌人的炮弹没有朝正确的方向飞行?_C#_Unity3d_Projectile - Fatal编程技术网

C# 敌人的炮弹没有朝正确的方向飞行?

C# 敌人的炮弹没有朝正确的方向飞行?,c#,unity3d,projectile,C#,Unity3d,Projectile,我很难让敌人的炮弹从敌人飞到玩家的位置。当我玩游戏时,敌人的子弹在屏幕上朝一个方向飞去,而不是朝玩家飞去。我认为问题可能在于我如何为射弹预制件指定方向?如有任何建议,将不胜感激 using System.Collections; using System.Collections.Generic; using UnityEngine; public class EnemyController : MonoBehaviour { public float speed; public

我很难让敌人的炮弹从敌人飞到玩家的位置。当我玩游戏时,敌人的子弹在屏幕上朝一个方向飞去,而不是朝玩家飞去。我认为问题可能在于我如何为射弹预制件指定方向?如有任何建议,将不胜感激

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

public class EnemyController : MonoBehaviour
{
    public float speed;
    public Rigidbody enemyRb;
    [SerializeField] float rateOfFire;
    private GameObject player;
    public GameObject projectilePrefab;
    float nextFireAllowed;
    public bool canFire;
    Transform enemyMuzzle;


    void Awake()
    {
        enemyRb = GetComponent<Rigidbody>();
        player = GameObject.Find("Player");
        enemyMuzzle = transform.Find("EnemyMuzzle");
    }

    void Update()
    {
        //move enemy rigidbody toward player
        Vector3 lookDirection = (player.transform.position - transform.position).normalized;
        enemyRb.AddForce(lookDirection * speed);

        //overallSpeed
        Vector3 horizontalVelocity = enemyRb.velocity;
        horizontalVelocity = new Vector3(enemyRb.velocity.x, 0, enemyRb.velocity.z);

        // turns enemy to look at player
        transform.LookAt(player.transform);

        //launches projectile toward player
        projectilePrefab.transform.Translate(lookDirection * speed * Time.deltaTime);
        Instantiate(projectilePrefab, transform.position, projectilePrefab.transform.rotation);

    }

    public virtual void Fire()
    {

        canFire = false;
        if (Time.time < nextFireAllowed)
            return;
        nextFireAllowed = Time.time + rateOfFire;

        //instantiate the projectile;

        Instantiate(projectilePrefab, enemyMuzzle.position, enemyMuzzle.rotation);

        canFire = true;
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类EnemyController:单行为
{
公众浮标速度;
公共刚体enemyRb;
[SerializeField]浮点数;
私人游戏对象玩家;
公共游戏对象投影;
允许下一次浮动;
公共场所火灾;
转换灌肠口;
无效唤醒()
{
enemyRb=GetComponent();
player=GameObject.Find(“player”);
灌肠口吻=transform.Find(“灌肠口吻”);
}
无效更新()
{
//将敌人的刚体移向玩家
Vector3 lookDirection=(player.transform.position-transform.position);
enemyRb.AddForce(注视方向*速度);
//全速
Vector3水平速度=enemyRb.velocity;
水平速度=新矢量3(enemyRb.velocity.x,0,enemyRb.velocity.z);
//将敌人转向玩家
变换。看(player.transform);
//向玩家发射射弹
projectleprefab.transform.Translate(lookDirection*speed*Time.deltaTime);
实例化(projectleprefab、transform.position、projectleprefab.transform.rotation);
}
公共虚拟空间火灾()
{
canFire=假;
如果(Time.Time
看起来实际发生的事情是创建了一堆项目符号,但没有存储对它们的引用。因此,当敌人靠近玩家时,每颗子弹都位于一个位置(这可能会让人觉得子弹相对于敌人移动)。我还假设敌人移动得非常快,因为它不是按增量时间缩放的,而是每帧更新一次

我认为ProjectlePrefab只是您正在生成的模板对象,所以您可能不想直接移动它,当然也不想每帧实例化一个新项目符号

如果要移动对象,则示例代码中产生的更改最少(但仍有问题)可能是:

public class EnemyController : MonoBehaviour
{
    // add a reference
    private GameObject projectileGameObject = null;
    void Update()
    {
        //Update position of spawned projectile rather than the template
        if(projectileGameObject != null ) {
            projectileGameObject.transform.Translate(lookDirection * speed * Time.deltaTime);
        }
       // Be sure to remove this extra instantiate
        //Instantiate(projectilePrefab, transform.position, projectilePrefab.transform.rotation);
    }

    public virtual void Fire()
    {
        //instantiate the projectile
        projectileGameObject = Instantiate(projectilePrefab, enemyMuzzle.position, enemyMuzzle.rotation);
    }
}
或者在列表中保留多个项目符号。此实现存在一个缺陷,即它总是使用当前敌方对玩家向量作为方向,而不是其发射时存在的方向


您最终可能希望的是,每个投射都有自己的类脚本来处理投射逻辑。enemyController类所要做的就是生成投射物,并将其方向和位置设置在一个单独的单行为上,该单行为存在于处理自身更新的投射物对象上。

您必须保存Instantiate()的返回值,并在那里进行转换您想一次发射一颗子弹吗?你想让子弹永远跟随玩家(就像一个“探索者”)?更好的方法(如果你想要多个子弹)是在子弹上放一个脚本,在更新()中移动它