【问题标题】:Bullets Shooting Only One Direction子弹只向一个方向射击
【发布时间】:2014-07-08 22:01:43
【问题描述】:

我正在使用 Unity 制作 2D 平台游戏,但遇到了一个小问题。我有向玩家发射子弹的敌人。但是,无论敌人面向哪个方向,子弹都只会向一个方向射击。

在我的敌人脚本中我有这个:

Instantiate(bullet, spawnPosition.position, Quaternion.identity);

在我的子弹脚本中我有这个

rigidbody2D.velocity = new Vector2(bulletSpeed,0);

如果可以,请提供帮助。 我理解为什么会发生这种情况,但我无法找到解决方案。为了更新我的问题,我希望能够检查敌人的方向,以便我可以将子弹速度更改为正/负以匹配方向。由于这种类型的敌人会有多个,我不知道该怎么做。

public class bulletScript : MonoBehaviour {

    // Use this for initialization
    private float bulletSpeed;
    GameObject parent;

    private Vector3 theScale;

    void Start () {

        rigidbody2D.velocity = new Vector2(bulletSpeed,0);
    }

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

    //  if(transform.localScale.x < 0) bulletSpeed = -100;
    //  if(transform.localScale.x > 0) bulletSpeed = 100;
    }
    public void SetEnemy(GameObject obj)
    {
        parent = obj;
    }

然后在 HammerScript.cs 中

    public class HammerScript : MonoBehaviour {


    public bulletScript bullet;
    public Transform spawnPosition;


    void FixedUpdate () 
    {

        instantiate(bullet, spawnPosition.position, Quaternion.identity);
        ((bulletScript)bullet).SetEnemy(this);                  
    }




    }

2 个新错误: 1-Assets/Scripts/Level 2/HammerScript.cs(89,64):错误CS1502:bulletScript.SetEnemy(UnityEngine.GameObject)' has some invalid arguments 2-Assets/Scripts/Level 2/HammerScript.cs(89,64): error CS1503: Argument#1'的最佳重载方法匹配无法转换HammerScript' expression to typeUnityEngine.GameObject'

【问题讨论】:

  • 请展示整个相关的代码部分(包括类声明)。
  • 我现在收到此错误:Assets/Scripts/Level 2/HammerScript.cs(88,48): error CS1061: Type UnityEngine.GameObject' does not contain a definition for SetEnemy' and no extension method SetEnemy' of type UnityEngine.GameObject ' 可以找到(您是否缺少 using 指令或程序集引用?)
  • 你应该把Enemy parent;改成GameObject parent;
  • 请再次检查我的回答。您需要将bullet 转换为bulletScript
  • 新的错误是什么?

标签: c# unity3d unityscript


【解决方案1】:

the bullets are only shooting in one direction regardless of which direction the enemy is facing.

好吧,除非bulletSpeed 根据敌人的方向而定为正或负,否则每颗子弹都将具有相同的速度和方向。在您的代码中,子弹的速度根本不取决于敌人的方向。

您可以做的是在子弹中保留对它来自的敌人的引用,然后根据该敌人的信息设置速度。

您可以通过在Bullet 类中使用Enemy 作为参数的SetEnemy 方法来做到这一点,然后您可以在Instantiate 调用之后立即调用((bulletScript)bullet).SetEnemy(this);

所以你的HammerScript 文件应该是这样的:

public class HammerScript : MonoBehaviour
{
   public bulletScript bullet;
   public Transform spawnPosition;

   void FixedUpdate () 
   {
       instantiate(bullet, spawnPosition.position, Quaternion.identity);
       ((bulletScript)bullet).SetEnemy(this);                  
   }
}

然后,在您的 Bullet 课程中,您可以拥有以下内容:

class bulletScript : MonoBehavior
{
    GameObject parent;
    public void SetEnemy(GameObject obj)
    {
        parent = e;
    }

    // ... whatever else you have, including the method that sets the velocity
}

然后在您的子弹脚本中,您可以将rigidbody2D.velocity 设置为与敌人有关的内容(即this.parent)。

【讨论】:

  • 我如何保留创建它的敌人的参考?我不想把它与同类型的多个敌人混淆
  • 这听起来像是可行的,但是我不确定如何引用创建子弹的原始对象。
  • 我对此还是有点陌生​​,所以我不完全确定创建 SetEnemy 方法是什么意思。
  • 谢谢!但是,当我使用 bullet.SetEnemy(this) 时,在敌人脚本中出现错误
  • 当我尝试输入 Enemy parent 和 (Enemy e) 时也出现错误
【解决方案2】:

您当前的实现只允许子弹进入一个方向。假设您的 bulletSpeed 始终为正数。

如果您考虑与该基本方向成角度alpha 的另一个方向,那么您需要创建一个旋转矢量,您可以这样做:

new Vector2(bulletSpeed * Mathf.Cos(alpha), bulletSpeed * Mathf.Sin(alpha));

还要考虑alpha 必须以弧度的形式给出,如果你真的想使用度数首先计算弧度:

alpha = (degrees / 180) * 90

【讨论】:

    猜你喜欢
    • 2020-05-15
    • 2014-01-07
    • 2017-05-22
    • 1970-01-01
    • 1970-01-01
    • 2021-11-18
    • 2014-02-21
    相关资源
    最近更新 更多