Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

I need some help

Discussion in '2D' started by mullet123, Apr 19, 2015.

  1. mullet123

    mullet123

    Joined:
    Apr 19, 2015
    Posts:
    7
    I am creating a side scrolling shooter of sorts with my character shooting projectiles. There are enemy characters set as prefabs that go across the screen. The problem is I am trying to get it so the projectile and enemy is destroyed when they hit each other. I put rigid bodies on both things. I gave a tag to the enemy object as "Enemy" directly in Unity and not in a script. In my script for the shot, I am trying to use the onCollisionEnter2D function and then search for the tag and destroy the object using

    void OnCollisionEnter2D(Collision2D other){
    if (other.gameObject.tag == "Enemy")
    {
    DestroyObject(other.gameObject);
    }
    }

    The problem is the code doesn't seem to work and both object pass through each other when they are hit. I can provide screenshots if anyone wants. I was wondering if someone might know something I could be missing or some code logic I'm not getting? Thanks
     
  2. mullet123

    mullet123

    Joined:
    Apr 19, 2015
    Posts:
    7
    The first image shows the settings for the enemy character in Unity. The second is the settings for the shot. The third image is the script for my shot.
    Capture.PNG Capture2.PNG Capture3.PNG
     
  3. biowinter

    biowinter

    Joined:
    Dec 10, 2011
    Posts:
    34
    Hey Mullet123

    You need to add a Collider2D to both objects such as BoxCollider2D or CircleCollider2D.
    Then set on the enemy the collider's IsTrigger to True

    Then on your code for the shot change the OnCollisionEnter2D() to OnTriggerEnter2D(Collider2D other)
    Unless you actually need Collision information.
     
  4. mullet123

    mullet123

    Joined:
    Apr 19, 2015
    Posts:
    7
    When would I need collision information?
     
  5. mullet123

    mullet123

    Joined:
    Apr 19, 2015
    Posts:
    7
    Also When I tried your suggestion, now every time I fire my shot the game resets. It goes back to the beginning state.
     
  6. biowinter

    biowinter

    Joined:
    Dec 10, 2011
    Posts:
    34
    You would need collision information for impact velocities or contact points but for the moment ignore these.

    Is it reloading when inputting a key or click or when objects collide? Collisions and Colliders are not related to reloading a level. Please look through your code and see if you are using the Application Class when calling your fire function.
     
  7. mullet123

    mullet123

    Joined:
    Apr 19, 2015
    Posts:
    7
    Another observation. If I add a box collider to just the enemy and not the shot, if my shot gets close to the enemy the game will reset. I have the code for the player below which has the code for resetting the game. I'm not sure what to make of this.... Capture.PNG
     
  8. mullet123

    mullet123

    Joined:
    Apr 19, 2015
    Posts:
    7
    I use application class when it uses the function "Die"
     
  9. biowinter

    biowinter

    Joined:
    Dec 10, 2011
    Posts:
    34
    By calling Die() on OnCollisionEnter2D() you are here saying that every time your player touches any surface including your shot and enemies the game will reset.

    If when firing your player overlaps with the shot ( too close to each other ) it will reset.
    You shouldn't have OnCollisionEnter2D without some Conditions. That is why it is getting reset every time your fire.
    Either you need to add the Conditions or to remove that line for the moment.

    Please contact me through PMs and Ill assist you further on this.
     
  10. mullet123

    mullet123

    Joined:
    Apr 19, 2015
    Posts:
    7
    Now it seems like whenever I have a collider for the enemy and the shot, everytime I click or hit the button to shoot, the game starts over. If I remove the collider from the the shot, it won't start over. 1.PNG 2.PNG