Search Unity

Question I want to make a box destroy when the player stands in front of it

Discussion in '2D' started by Skyflamme, Feb 25, 2021.

  1. Skyflamme

    Skyflamme

    Joined:
    Aug 12, 2020
    Posts:
    33
    Hello, I'm a new User of unity and the programming language of C#. Now I have a problem. I want to make that the Player can destroy boxes when he is standing in front of it and pressing the left mouse button. Now I have tried OnCollision2D, OnCollision and OnCollisionStay, it worked sometimes then when impressing the mouse button and a second is going gone. That's really awful, can anybody help me?
     
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,465
    Can you post your code? Use code tags please so it is easier to read (look at the text ribbon where it says code)
     
  3. Skyflamme

    Skyflamme

    Joined:
    Aug 12, 2020
    Posts:
    33
    I don't have the code that I wrote before but now I have this:


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Destroy : MonoBehaviour
    6. {
    7.     private void OnCollisionStay2D(Collision2D collision)
    8.     {
    9.         string tag = collision.gameObject.tag;
    10.         Debug.Log(tag);
    11.         if (Input.GetMouseButtonDown(0))
    12.         {
    13.             if (collision.gameObject.tag.Equals("desBox"))
    14.             {
    15.                 Destroy(collision.gameObject);
    16.             }
    17.         }
    18.     }
    19. }
    The code is on my Player
     
  4. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,465
    Is the function calling at all? Aka, is that first Debug.Log(tag) working?

    Do both the Player and the Box to be destroyed have Collider2Ds on them? Make sure neither are set as triggers. One of the objects must have a Rigidbody2D as well.
     
  5. Skyflamme

    Skyflamme

    Joined:
    Aug 12, 2020
    Posts:
    33
    The Debug.Log(tag) is working, but the Stay Method is checking it every 1 seconds, that's very bad
     
  6. DaRealXDev

    DaRealXDev

    Joined:
    Feb 5, 2021
    Posts:
    13
    Why are you even using collisions? Aren't you just checking if a player is near the box?
     
  7. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,465
    To add on to Xavier, you can just use OnTriggerEnter2D instead of OnCollision.
     
  8. DaRealXDev

    DaRealXDev

    Joined:
    Feb 5, 2021
    Posts:
    13
    Why not use a raycast or magnitude?
     
  9. Skyflamme

    Skyflamme

    Joined:
    Aug 12, 2020
    Posts:
    33
    The boxes has no Collisions anymore, how i can fix that?
     
  10. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,465
    If you are using OnTriggerEnter2D make sure you mark the collider as trigger in the inspector.
     
  11. Skyflamme

    Skyflamme

    Joined:
    Aug 12, 2020
    Posts:
    33
    Yeah but there is no Collision, its not like a wall, I can go towards it, its like noclip
     
  12. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,465
    So i dont really understand what that means. What is the code you have now?
     
  13. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,465
    Okay, maybe i know you mean now. That the player isn't hitting a "wall" or the bounds of the collider. That is what happens when you mark a collider as trigger. But when you use the OnTriggerEnter2D or Stay2D with it, the collider still detects something has come in contact, it just doesnt stop them physically.
     
  14. Skyflamme

    Skyflamme

    Joined:
    Aug 12, 2020
    Posts:
    33
    and what I have d oto
     
  15. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,465
    ??? Is it working now? If not, can you post your code?