Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.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# 如何使刚体2D项目符号在Unity2D中移动_C#_Unity3d - Fatal编程技术网

C# 如何使刚体2D项目符号在Unity2D中移动

C# 如何使刚体2D项目符号在Unity2D中移动,c#,unity3d,C#,Unity3d,几天来我一直在尝试各种修复方法,但我无法解决这个问题。我需要一颗子弹来繁殖和移动直到碰撞,但我甚至不能让它移动(它繁殖得很好)。注:这是一个2D游戏。 这是我的密码: public class Enemy : MonoBehaviour { public float timeBetweenFire = 2; public Rigidbody2D bullet; public Transform gunSpawn; private float timer;

几天来我一直在尝试各种修复方法,但我无法解决这个问题。我需要一颗子弹来繁殖和移动直到碰撞,但我甚至不能让它移动(它繁殖得很好)。注:这是一个2D游戏。 这是我的密码:

public class Enemy : MonoBehaviour
{
    public float timeBetweenFire = 2;
    public Rigidbody2D bullet;
    public Transform gunSpawn;
    private float timer;
    // Start is called before the first frame update
    void Start()
    {
    }

    // Update is called once per frame
    void Update() {   
        timer+=Time.deltaTime;
        if (timer >= timeBetweenFire) {
            Shoot();
            timer = 0;
        }
    }

    private void Shoot() {
        Rigidbody2D bulletInstance;
        bulletInstance = Instantiate(bullet, gunSpawn.position, gunSpawn.rotation) as Rigidbody2D;
        //bulletInstance.AddForce(gunSpawn.forward * 1000f);
        //bulletInstance.AddForce(Vector3.up * 10 * Time.deltaTime);
        //bulletInstance.AddForce(transform.forward * 100);
        bulletInstance.AddForce(Vector3.up * 1000);
    }
}
注释的bulletInstance方法是我最近尝试过的,我对bullet做了大量的代码更改(iirc在刚体、游戏对象和变换之间进行了更改),但没有任何帮助。子弹与任何东西都有一段距离,除了枪炮,枪炮只是一个空的游戏对象,所以我不认为碰撞是一个问题。我是一名新的Unity程序员,请原谅我犯的任何愚蠢的错误

更新: 这就是我所做的,让它(大部分)工作。当子弹相撞时,它可能会旋转出来,但至少我取得了一些进展

public class Deflect : MonoBehaviour
{
    private Rigidbody2D bullet;
    public float bulletSpeed = 0.1f;
    Vector2 direction = new Vector3(0.7f,0.7f);
    ContactPoint2D[] myContact = new ContactPoint2D[1];
     Vector2 _velocity;
    public void Update ()
    {
    }


    // Start is called before the first frame update
    void Start()
    {
        bullet = this.GetComponent<Rigidbody2D>();


        _velocity = bullet.velocity;
    }

    void OnCollisionEnter2D(Collision2D collision)
    {
        foreach (ContactPoint2D contact in collision.contacts)
        {
            print(contact.collider.name + " hit " + contact.otherCollider.name);
            // Visualize the contact point
            Debug.DrawRay(contact.point, contact.normal, Color.white);
        }

        Vector2 inNormal = collision.contacts[0].normal;
        _velocity = Vector3.Reflect(_velocity, inNormal);
        bullet.velocity = _velocity;
    }
}
公共类偏离:单一行为
{
私有刚体2D子弹头;
公共交通速度=0.1f;
矢量2方向=新矢量3(0.7f,0.7f);
ContactPoint2D[]myContact=新的ContactPoint2D[1];
矢量2_速度;
公共无效更新()
{
}
//在第一帧更新之前调用Start
void Start()
{
bullet=this.GetComponent();
_速度=子弹速度;
}
空心OnCollisionInter2D(碰撞2D碰撞)
{
foreach(ContactPoint2D contact in collision.contacts)
{
打印(contact.collider.name+“hit”+contact.otherCollider.name);
//可视化接触点
Debug.DrawRay(contact.point、contact.normal、Color.white);
}
Vector2 inNormal=碰撞。触点[0]。正常;
_速度=矢量3.反射(_速度,正常);
bullet.velocity=_速度;
}
}

确保子弹有一个刚体组件,并且没有设置为运动学。

1-将你的子弹预设为
游戏对象

2-向其添加一个
rigiidbody2d

3-确保在
Rigibody2d

4-然后
GameObject bulletInstance=实例化(bullet、gunsawn.position、gunsawn.rotation);
bulletInstance.GetComponent().AddForce(Vector3.up*1000)


IsKinetic可防止
刚体2d
受到力的影响。

当我尝试使用您建议的代码时,它会说“不能隐式地将类型'UnityEngine.Rigidbody2D'转换为'UnityEngine.GameObject'”。将您的项目符号声明为GameObject而不是Rigidbody2D,一切都应该正常。确保遵循我在考试前遇到的步骤code@mahdiOP应该反过来声明
bullet
bulletInstance
Rigidbody2D
。。否则,
bulletInstance.AddForce
将失败。。。但我想这就是OP最初所做的,对吧?;)伍普西,我的错!我将编辑我的回答。我忘记了。GetComponent()我用我现在拥有的更新了问题。到目前为止,它在正确的角度偏转,只是旋转一点。有没有办法防止物理学影响旋转?禁用它是否意味着我需要自己计算旋转?是动态的还是静态的?当使用力时,动态是最好的方法。动态物体将在物理模拟(有限质量等)中表现出预期的行为。静态物体在模拟过程中不会移动,就好像它们具有无限质量一样。以下是有关此主题的文档:预制件上是否启用了
iskinetic
?我不会在这里使用武力。你总是要考虑子弹的质量。直接设置固定速度:
bulletInstance.velocity=…
然后还要注意
Vector3.up
是一个全局方向。。如果您希望依赖于
gunsawn
的旋转,则应使用例如
bulletInstance.velocity=gunsawn.up*bulletVelocity