简体   繁体   English

用子弹射击时物体未被破坏-UNITY3D C#

[英]Object not being destroyed when shot with a bullet - UNITY3D C#

So in my game, there's a gun that sprays bullets, and I'm trying to make a gameObject destroy on collision with the bullets. 因此,在我的游戏中,有一杆枪可以喷射子弹,而我试图使gameObject在与子弹碰撞时被摧毁。 The bullets are based off of one gameObject (Capsule). 项目符号基于一个gameObject(胶囊)。 I've tried these two scripts so far: 到目前为止,我已经尝试了以下两个脚本:

using UnityEngine;
using System.Collections;

public class whenshot : MonoBehaviour {

void OnCollisionEnter(Collision col)
{
    if (col.gameObject.name == "Bullet")
    {
        Destroy(col.gameObject);
    }
}
}

and: 和:

using UnityEngine;
using System.Collections;

public class whenshot : MonoBehaviour {


void OnCollisionEnter(Collision col)
{
    if (col.gameObject.name == "Bullet")
    {
        Destroy(this); //the difference between the two is that I changed "col.gameObject" to "this"
    }
}
}

I'm shooting the object but it's not disappearing/destroying itself. 我正在拍摄物体,但它并未消失/破坏。 How can I fix this? 我怎样才能解决这个问题?

Here's a visual if it helps: 如果有帮助,请看以下图片: 视觉效果

this refers to the object instance of the caller ( this is basic OOP ), ie, whenshot , and not gameObject . this是指调用者的对象实例( 这是基本的OOP ),即whenshot ,而不是gameObject So the second sample is effectively Destroy ing the instance of the script from the gameObject it is attached to. 因此,第二个示例是有效地从附加到其上的gameObject Destroy脚本的实例。

The first script is technically fine, and should work, provided these conditions are met: 第一个脚本在技术上很好,并且可以满足以下条件:

  1. Either the projectile (bullet) or the target (or both) have a non-kinematic rigidbody component attached. 子弹(子弹)或目标(或两者)都附有非运动刚体组件。 ( Unity docs. ) Unity文档。
  2. Both have 3D Collider components. 两者都具有3D Collider组件。
  3. The name of every single bullet gameObject that collides with the target is exactly "Bullet". 与目标相撞的每个子弹游戏对象的名称都恰好是 “子弹”。
  4. All projectile objects have this script as a component. 所有弹丸对象都将此脚本作为组件。

Some suggestions 一些建议

Use prefabs and tags: take your bullet primitive and store it as a prefab. 使用预制件和标签:将您的子弹原语存储为预制件。 Add a tag to the prefab called "Bullet". 在预制件上添加一个名为“子弹”的标签。 Do the same for the target and tag it as "Target". 对目标执行相同操作,并将其标记为“目标”。 Tag the player as "Player". 将播放器标记为“玩家”。 In the "gunController", set a reference to the bullet prefab and make it Instantiate bullets on whatever trigger you're using. 在“ gunController”中,设置对子弹预制件的引用,并使其在您使用的任何触发器上实例化子弹。 In the bullet 's script, use CompareTag("Target") instead of == and Destroy both the target gameObject and this.gameObject. 项目符号的脚本中,使用CompareTag("Target")代替==并销毁目标gameObject和this.gameObject。

It seems to me that the above is the behaviour you want. 在我看来,以上是您想要的行为。 If that is the case, there is no delay between collision and destruction, and hence no need to simulate physics whatsoever. 在这种情况下,碰撞与破坏之间不会存在延迟,因此无需模拟任何物理过程。 Unless you have some other physics interactions with bullets/targets, mark the one without a rigidbody as a Trigger. 除非您与子弹/目标发生其他物理相互作用,否则将没有刚体的物体标记为触发器。

A Strong Suggestion 强烈建议

Go through Unity tutorials. 查看Unity教程。

This is an example from a 2D Game I made a while back, but i think it might help. 这是我前一段时间制作的2D游戏的一个示例,但我认为这可能会有所帮助。

void OnTriggerEnter2D(Collider2D other)
{
    if (other.tag == "Destroyable")
    {
        Destroy(other.gameObject);
    }
}

I used this to destroy certain blocks when the player would shoot them so many times, just switch them to the 3D Collider and Trigger, but it should do the trick for ya (i hope ^^). 当玩家将它们射击很多次时,我用它来摧毁某些方块,只需将它们切换到3D对撞机和扳机上即可,但是它应该可以解决问题(我希望^^)。

edit: this script should be attached to your bullet prefab 编辑:此脚本应附加到您的子弹预制件上

Ok so I figured it out, it's kind of weird but apparently I was making the bullets move too fast... I had to slow down the "Bullet_Forward_Force" float to about 150f to make it work. 好的,所以我弄清楚了,这有点奇怪,但是显然我是在使子弹移动得太快了……为了使它起作用,我不得不将“ Bullet_Forward_Force”浮子的速度减慢到大约150f。 Thanks to everyone who answered though. 感谢所有回答的人。

I can't comment so I will make an answer: 我无法发表评论,所以我会回答:

You can make the bullet go fast, just set the collision detection to continious dynamic. 您可以使子弹快速移动,只需将碰撞检测设置为连续动态即可。

It has an almost %100 success rate. 它的成功率接近100%。

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

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