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. Dismiss Notice

If there is no collision...

Discussion in 'Scripting' started by cate5171, Oct 16, 2020.

  1. cate5171

    cate5171

    Joined:
    Jan 5, 2020
    Posts:
    101
    Hello Everyone,
    I am wondering about Non-Collision actions. Like, if there is no collision, a action could be played. But if there is collision, that action can not be played at that time. I have researched online and the closest thing i have seen is "OnCollisionExit" which happens if you leave the collision of a object.
    Is it possible to make this possible?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Can you describe more what you're trying to accomplish?

    What event are you trying to trigger some action from? If you just want to do something every frame as long as your object is not colliding with anything, you could do something like this:
    Code (CSharp):
    1. HashSet<Collider> currentlyTouching = new HashSet<Collider>();
    2.  
    3. void OnCollisionEnter(Collision c) {
    4.   currentlyTouching.Add(c.collider);
    5. }
    6.  
    7. void OnCollisionExit(Collision c) {
    8.   currentlyTouching.Remove(c.collider);
    9. }
    10.  
    11. void Update() {
    12.   if (currentlyTouching.Count > 0) {
    13.     // This code will run if you are touching at least one object    
    14.     Debug.Log("I'm currently touching " + currentlyTouching.Count + " objects!");
    15.   }
    16.   else {
    17.     // This code will run when you are NOT touching ANYTHING.
    18.     Debug.Log("I'm not touching anything!");
    19.   }
    20. }
     
  3. cate5171

    cate5171

    Joined:
    Jan 5, 2020
    Posts:
    101
    In my game, i am making a building system where you can place stuff and etc. This post is helping me with a over-lapping problem i am having where you can place blocks inside of each other. So i wanted to create a script so the block can be placed IF there is no collision but if there is, the block can not be placed
     
  4. cate5171

    cate5171

    Joined:
    Jan 5, 2020
    Posts:
    101
    I have tested the script but it has 4 errors mainly saying Collider has no definition for (Add, Remove, Count)
     
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    If you're talking about placing blocks it might make more sense to use a test like Physics.CheckBox to see if anything overlaps a given box:

    https://docs.unity3d.com/ScriptReference/Physics.CheckBox.html
     
    Vryken likes this.
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    You must have mis-copied this line then:
    Code (CSharp):
    1. HashSet<Collider> currentlyTouching = new HashSet<Collider>();
     
  7. cate5171

    cate5171

    Joined:
    Jan 5, 2020
    Posts:
    101
    They aren't really blocks, I am using prefabs of buildings/trees/other stuff
     
  8. cate5171

    cate5171

    Joined:
    Jan 5, 2020
    Posts:
    101
    Unrelated note, is there any way to stop box colliders being solid? (I can't test it, i added a box colldier to the "Building things" and set them to trigger but they are just acting normal)
     
  9. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Exactly as you noted, set the Collider as a trigger. If you do that you'll have to use OnTriggerEnter/Exit etc. instead on OnCollisionEnter etc. in your scripts.