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

Question How to collide without a Box Collider?

Discussion in 'Scripting' started by alainjiehfeng, Jun 6, 2020.

  1. alainjiehfeng

    alainjiehfeng

    Joined:
    May 13, 2020
    Posts:
    60
    Plain and simple, I want a way to detect if my player picks up an item, but that item shouldn't have a box collider as the player should be able to walk through it as with any game.

    But at the same time I need to detect if the player has touched that item. How do I detect this while keeping the box collider component turned off so that I can get the player to be able to walk through it? :)
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    You can mark the collider with a layer that does not collide with the player but is detectable by raycast. You can configure physics to not collide any two arbitrary layers (Edit-> ProjectSettings-> Physics).

    OR you can just use Vector3 to see if the player is close enough using Vector3.Distance().
     
    alainjiehfeng likes this.
  3. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    You could also use a trigger collider.
     
    jenil97 and leftshoe18 like this.
  4. alainjiehfeng

    alainjiehfeng

    Joined:
    May 13, 2020
    Posts:
    60
    Thank you both!
     
  5. alainjiehfeng

    alainjiehfeng

    Joined:
    May 13, 2020
    Posts:
    60
    I just did the first method you told, I unchecked the box where the player's layer and the pickable meet. It works wherein that I can go through it, but the collision doesn't get detected. :(
     
  6. chrische5

    chrische5

    Joined:
    Oct 12, 2015
    Posts:
    52
    Hello

    Set "IsTrigger" to true and you can walk trough but you also can detect a collision.

    christoph
     
    alainjiehfeng likes this.
  7. alainjiehfeng

    alainjiehfeng

    Joined:
    May 13, 2020
    Posts:
    60
    I just tried it but it still doesn't detect as there is no touching between the two objects. Maybe I'm missing something?

    I am using OnTriggerEnter in your case.
     
  8. chrische5

    chrische5

    Joined:
    Oct 12, 2015
    Posts:
    52
    Hello


    Try OnTriggerEnter2D and check the size of the Colliers.

    Christoph
     
  9. alainjiehfeng

    alainjiehfeng

    Joined:
    May 13, 2020
    Posts:
    60
    Tried that as well, still no dice. :(
     
  10. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Do you still have the collision layer between the player and the pickable unchecked?
    You would have to re-check the box, since you're now trying to detect intersections between these layers.
     
    alainjiehfeng likes this.
  11. alainjiehfeng

    alainjiehfeng

    Joined:
    May 13, 2020
    Posts:
    60
    Ah I see, but I just re-checked it and I am still able to move through it and the collision detection isn't working. Hmm...
    Maybe I have to do something with the new layer I made? Sorry, new to this.
     
  12. leftshoe18

    leftshoe18

    Joined:
    Jul 29, 2017
    Posts:
    61
    For trigger collision to be detected one of the objects needs to have a Rigidbody on it (set it to kinematic and don't use gravity if you want to control how it moves). You then need to use OnTriggerEnter or OnTriggerEnter2D (depending if you're doing 3D or 2D) and put your collision code in there.
     
    Irtaza-2009 likes this.
  13. alainjiehfeng

    alainjiehfeng

    Joined:
    May 13, 2020
    Posts:
    60
    The player has a rigidbody, but since the player moves through the pickable (which has its own layer after the player), the collision doesn't work. I tried everything that was said here and what I got from it so far is that the collision doesn't detect if the player doesn't touch the physical object, so when there's a layer and the player could go through the object, collision doesn't get detected. I need a way around this, maybe the layer I made and the position of the layer is wrong?
     
  14. alainjiehfeng

    alainjiehfeng

    Joined:
    May 13, 2020
    Posts:
    60
    Nvm, my bad, I fixed it. The mistake was:

    1. I didn't uncheck the box in the physics setting as @Vryken said.

    It still didn't work.

    2. I had to then change the OnTriggerEnter variable type from Collision to Collider, which then works!

    Thank you all. :D
     
    Kurt-Dekker and leftshoe18 like this.