Unity游戏对象并没有向前发展。 (太空射击)

时间:2016-03-22 01:48:09

标签: c# unity3d

我是Unity的新手,所以我只是习惯了所有的库等。我正在尝试创建一个太空射击游戏,现在正在创建子弹。我现在可以生成子弹,现在的问题是它不会向我的船只前进。任何帮助将不胜感激。谢谢。

以下是代码:

    protected boolean isSafeTag(String tag){
      Boolean retVal = true;
    Document doc = Jsoup.parse(tag);
    if (doc.getAllElements().size()>0){
        Element e = doc.getAllElements().get(0);
        String attribute =  e.attr("class");
          if ((attribute != null) && (attribute.contains("ad") ||   attribute.contains("nav"))){
            retVal = false;
           }
       }
       if (retVal == false)
          return false
       else
          return super.isSafeTag(tag);

使用System.Collections;

public class Projectile:MonoBehaviour {

using UnityEngine;

}

2 个答案:

答案 0 :(得分:1)

使用transform.Forward()* Speed;而不是变换方向。 http://docs.unity3d.com/ScriptReference/Transform-forward.html

另外,请查看this link,因为您应尝试使用ApplyForces而不是直接修改力度

答案 1 :(得分:0)

您想使用Rigidbody.ApplyForce();

对子弹刚体施加力

此外,您的代码目前显示较差的OO架构,因此我还建议在空的gameObject或太空船上创建一个新脚本,它将管理播放器输入。然后让弹丸管理必需品。例如:

// Attached to Either empty GO or spaceship GO
public class PlayerInput : MonoBehaviour {
    public GameObject spaceship;
    public GameObject bulletPrefab;
    public float projectileSpeed;
    void Update()
    {
        if(Input.GetButtonDown("Fire1"))
        {
            GameObject newBullet = Instantiate(bulletPrefab, spaceship.transform.position, new Quaternion()) as GameObject;
            newBullet.GetComponent<Rigidbody>().AddForce(spaceship.transform.forward * projectileSpeed);
        }
    }
}

// Attached to Either Projectile GO
public class Projectile : MonoBehaviour {

    void OnCollisionEnter()
    {
        Debug.Log("Boom");
    }

}

另请注意,您可能需要调整刚体属性以获得所需的效果,例如禁用重力。

相关问题