Home > Blockchain >  Unity2D c#: I have a problem with two colliders on the enemy
Unity2D c#: I have a problem with two colliders on the enemy

Time:01-06

I have a problem with the enemy in my game. The enemy goes to the left and I want if the player jumps on the enemy then the enemy dies. Or if the enemy collides with the player on the side then the player dies. I made the script only atttached on the enemy. But if the enemy collides on the side the player dies and if I jump on the enemy the player still dies.

Here is the code of the enemy:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyMovement : MonoBehaviour
{
    public float range = 10f; // the range at which the enemy will start moving towards the player
    public float speed = 5f; // the speed at which the enemy will move

    private Transform player; // reference to the player's transform
    private Rigidbody2D rb; // reference to the enemy's Rigidbody2D component

    void Start()
    {
        // find the player's transform by finding the object with the "Player" tag
        player = GameObject.FindGameObjectWithTag("Player").transform;

        // get the enemy's Rigidbody2D component
        //rb = GetComponent<Rigidbody2D>();
    }

    void Update()
    {
        // calculate the distance between the enemy and the player
        float distance = Vector2.Distance(transform.position, player.position);

        // if the distance between the enemy and the player is less than the range
        if (distance < range && Pause.IsPause == false)
        {
            // move the enemy towards the player at the specified speed
            transform.position  = Vector3.left * speed * Time.deltaTime;
            distance = Vector2.Distance(transform.position, player.position);
            if (distance > range)
            {
                Destroy(gameObject);
            }
        }
    }

    void OnCollisionEnter2D(Collision2D collision)
{
    if (collision.gameObject.tag == "Player")
    {
        // The enemy has collided with the player
        if (collision.relativeVelocity.y > 0 && transform.position.y < collision.transform.position.y)
        {
            // The player is colliding with the top of the enemy from above
            // Destroy the enemy game object
            Destroy(gameObject);
        }
        else
        {
            // The player is colliding with the side or bottom of the enemy, or the player is not colliding with the top of the enemy from above
            // Destroy the player game object
            Destroy(collision.gameObject);
        }
    }
}



}

ChatGPT helped me but it failed with the colliders. Can somebody help me please?

CodePudding user response:

If Collision.relativeVelocity works the way I think it does, that means you need to replace collision.relativeVelocity.y > 0 with collision.relativeVelocity.y < 0.

If the relativeVelocity is based on the reference frame of the enemy, then it is the velocity of the player relative to the enemy. If the player's relative velocity is > 0, then the player would still be rising, not falling. This means the code will never run if the player is falling. Changing it to < 0 makes it so the code will only run if the player IS falling.

You can test my assumption using Debug.Log(collision.relativeVelocity.y) when you collide with the top. If y is negative, then it's true.

  • Related