简体   繁体   English

2d Unity 射击错误

[英]2d Unity Shooting bug

i'm actually trying to make a sort 2d game with unity, in this game we control a character that can shoot bullets with a weapon.我实际上是在尝试制作一个具有统一性的 2d 游戏,在这个游戏中,我们控制一个可以用武器射击子弹的角色。 Actually an "Uzi", so it shoot very fast and hard,(so, a gun) the problem is, that sometimes, depending on the distance with the ennemy, the bullets go trough the box collider of the ennemy, and so don't touch it.实际上是一个“Uzi”,所以它射得又快又硬,(所以,一把枪)问题是,有时,根据与敌人的距离,子弹会穿过敌人的盒子对撞机,所以不要触摸它。 I think it has to do with the Update/ Fixed update system of unity, but i'm not sure.我认为这与统一的更新/固定更新系统有关,但我不确定。 and The bullets are already in collision detection Continuous.和子弹已经在碰撞检测连续。 There is a part of the code, and screenshots.有一部分代码,还有截图。

 void OnTriggerEnter2D(Collider2D target)
{
    if (target.gameObject.tag == "FirePoint")
    {
        Fire();
    }

    if (target.gameObject.tag == "Building")
    {
        Destroy(gameObject);
        //print("Don't shoot on the walls !");
    }

    Ennemy enemy = target.GetComponent<Ennemy>();
    if (target.gameObject.tag == "Ennemi")
    {
        enemy.TakeDamage(damage);
        Destroy(gameObject);

    }

}



void Fire()
{
    GetComponent<Rigidbody2D>().AddForce(transform.right * bulletForce);
}

and there is the method for shooting, in another script.还有拍摄的方法,在另一个脚本中。

public void Shoot()
{
    DispersionDesBalles();
    nextFire = Time.time + 1f / fireRate;
    Instantiate(Balle, spawnPoint.position, fireRotation);

    ballesRestantes--;
}

and screenshots of the bug Here和错误的截图在这里

The red rectangle are ennemies, and the little green things are bullets, don't pay attention to the graphics :D, It's just for testing.It's pretty annoying because in the game, the bullets are less spaced apart, but it amounts to the same thing.红色矩形是敌人,绿色小东西是子弹,不要注意图形:D,只是为了测试。很烦人,因为在游戏中,子弹间距较小,但相当于一样。

I hope someone can help me.我希望有一个人可以帮助我。 have a Nice day !祝你今天过得愉快 !

Yes, it has something to do with the physics engine refresh rate which is not in sync with rendering.是的,它与与渲染不同步的物理引擎刷新率有关。 In case of small and fast colliders you will get issues like this.在小而快速的碰撞体的情况下,你会遇到这样的问题。 There are different ways to deal with it but the easiest would be to increase your bullet collider size to make sure it is not passing trough.有不同的处理方法,但最简单的方法是增加子弹对撞机的尺寸以确保它不会通过。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM