Chrizzly
Chrizzly

Reputation: 43

Enemy is destroyed without actually hitting the trigger

I'm trying to make a 2D Sidescroller in Unity right now and have some problems regarding the enemy.

In theory I want the enemy be destroyed if the player jumps on it's head(upper collider/trigger) and the player loses health if he hits the other collider/trigger.

Well the player loses health when hitting the enemy from the side but it also destroys the enemy and I don't know why. It isn't even near the deathCollider.

Upper collider tag(isTrigger) : enemyHit

(this one is placed at the top of the enemy)

Bigger center collider tag(isTrigger) : enemy

(this one is under the other collider and encloses the rest of the body)

Sorry can't post an image :/

My onTrigger method in the playerScript looks like this

    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);
    }
}

And the enemyScript only has it's movement

    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);
}

Edit:

the GotHit() method looks like this

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

Thanks in advance :)

Chrizzly

Upvotes: 0

Views: 187

Answers (1)

Chrizzly
Chrizzly

Reputation: 43

I found the reason...you were right - I have a script that gives the player point for defeating an enemy and there was still some test code that destroyed the complete GameObject -.- Thanks for the help :) –

Upvotes: 0

Related Questions