统一摧毁敌人摧毁错误的一个混淆

时间:2014-10-27 01:12:45

标签: unity3d unityscript

我已经尝试了一切来使毁灭工作毁灭(敌人)毁灭(敌人)毁灭(enemy.gameObject .....等所有缩写都不会工作。 有人可以帮忙吗???

我可以与所有物体碰撞并且它们会破坏但是如果屏幕上有2个敌人我击中了离我最近的那个,另一个会死,因为它最后产生了我怎么能摧毁我跳的那个?心灵efukd。 :(

var Player : GameObject;
var Gravity:float = 2;
var speed:float = 2;
var enemytrans : Transform;
var enemy: GameObject;
public var jumped = false;
var anim : Animator;

function Start () {
     while (true) {
         yield WaitForSeconds (Random.Range(3, 0));
         enemy = Instantiate(enemytrans).gameObject;
     }
anim = GetComponent(Animator);
 // Instantiate the prefab and put the gameObject into "theCube"
}

function Update () {


Player.transform.position.x = -4.325;
//the gravity function

if (jumped == false){
anim.SetFloat("hf",0.0);
}
if (Input.GetButtonDown("Fire1") && jumped==false){
fire();
jumped = true;
}
if(jumped==true){
anim.SetFloat("hf",1);
}
}


function OnCollisionEnter2D(coll: Collision2D) {
if(coll.gameObject.CompareTag("ground")){
anim.SetFloat("hf",0.0);
jumped=false;
}
if(coll.gameObject.CompareTag("enemy") && jumped==true){
fire(); 
jumped=true;
Destroy(enemy,1);***********************************************this line************
}
if(coll.gameObject.CompareTag("enemy") && jumped==false){
Destroy(Player);
}
}


function fire(){
Player.transform.Translate(Vector3(Input.GetAxis("Vertical") * speed * Time.deltaTime, 0, 0));
Player.rigidbody2D.velocity = Vector2(0,10);
}

1 个答案:

答案 0 :(得分:1)

问题

您正在销毁最后生成的GameObject,您在enemy变量中保留了对它的引用。


解决方案

你应该摧毁你击中的东西。 Unity3D已经为您提供了与之相撞的人,因此只需使用该信息即可。

function OnCollisionEnter2D(coll: Collision2D) 
{
    if(coll.gameObject.CompareTag("enemy") && jumped==true)
       Destroy(coll.gameObject);
}