敌人被摧毁而没有实际击中扳机

时间:2015-03-23 13:17:21

标签: c# unity3d

我正在尝试在Unity中制作2D Sidescroller并且在敌人方面遇到一些问题。

理论上我希望如果玩家跳到它的头部(上部对撞机/触发器)并且如果他击中另一个碰撞器/触发器时玩家失去健康,则会摧毁敌人。

玩家从侧面击中敌人时会失去健康,但它也会摧毁敌人,我不知道为什么。 它甚至不在死亡骑士附近。

上部对撞机标签(isTrigger):enemyHit

(这个位于敌人的顶端)

更大的中心对撞机标签(isTrigger):敌人

(这个在另一个对撞机下面,并包围身体的其余部分)

抱歉无法发布图片:/

playerScript中的onTrigger方法如下所示

    void OnTriggerEnter2D(Collider2D other){
    if (other.tag == "spike") {
        GotHit();
    }
    if (other.tag == "groundTrap") {
        InstandDeath();
    }
    if (other.tag == "enemy") {
        //hitEnemy = other.gameObject;
        AudioSource.PlayClipAtPoint(hitSound, transform.position, 10.0f);
        GotHit();
    }
    if (other.tag == "enemyHit") {
        rigidbody2D.velocity = Vector2.zero;
        rigidbody2D.AddForce (jumpVector, ForceMode2D.Force);
        //switch animation to 'jump'
        anim.SetInteger("AnimationStatePink", 2);
        AudioSource.PlayClipAtPoint(jumpSound, transform.position, 10.0f);
        Destroy(other.gameObject);
    }
}

敌人的游戏只有它的运动

    public float enemySpeed;
private Vector3 movement;
public static bool startEnemyMovement = true;
//set walking limits
public float leftLimit, rightLimit;
//adjust scaling
public float scaleX, scaleY;
public float rotateX, rotateY;

public void Start(){
    movement = Vector3.left * enemySpeed;
}

public void Update() {

    if (startEnemyMovement == true)
        EnemyMovement ();

    if (PinkPlayerControler.isPaused == true)
        startEnemyMovement = false;
    else
        startEnemyMovement = true;
}

void EnemyMovement(){
    if (this.transform.position.x > rightLimit) {
        movement = Vector3.left * enemySpeed;
        transform.localScale = new Vector3(-scaleX,scaleY,1);
    } else if (this.transform.position.x < leftLimit) {
        movement = Vector3.right * enemySpeed;
        transform.localScale = new Vector3(scaleX,scaleY,1);
    }
    transform.Translate(movement);
}

编辑:

GotHit()方法看起来像这样

    void GotHit (){
    if(Health.gameLives > 1){
        Health.gameLives--;
        AudioSource.PlayClipAtPoint(hitSound, transform.position, 10.0f);
    }
    else {
        youLose();
    }
}

提前致谢:)

Chrizzly

1 个答案:

答案 0 :(得分:0)

我找到了原因......你是对的 - 我有一个脚本给玩家点击击敌人,还有一些测试代码摧毁了完整的GameObject -.-感谢你的帮助:) - < / p>