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

Collision detection not working - 2D sprites

Discussion in 'Scripting' started by disband85, Dec 9, 2010.

  1. disband85

    disband85

    Joined:
    Dec 7, 2010
    Posts:
    83
    Hey all,

    I'm trying to get collision detection working for sprites (I'm using SM2).

    Both sprites have a Box Collider. I have ticked 'Is Trigger' for both of them. I have set X and Y to the width and height of the sprite for both of them, and set Z to 3 just so it has some sort of thickness.

    One of the sprites has a rigid body. I unticked 'Use Gravity' and left the other settings as default.

    Here is my code in the script that's attached to the object with the rigid body:

    Code (csharp):
    1. public void OnTriggerEnter(Collider c)
    2.     {
    3.         Debug.Log("Collided!");
    4.     }
    Now the scenario. The object that doesn't have the rigid body is actually a prefab, I am creating clones of it every few seconds and moving them across the screen. If they hit the other object (the one with the rigid body), I should get a "Collided!" message in the console. It works IF i move the rigid body object (with mouse input) AFTER the clone has spawned and they collide. However, if a clone spawns, moves across the screen and hits the rigid body object, and I have not moved the rigid body object (via mouse input), then there's no "Collided!" message, hence no collision. Any ideas why?

    Note: When the collision works, the rigid body object is not moving at the time of collision. It has stopped moving by the time the clone hits it, and collision works. If I then leave the rigid body object in its place, and another clone spawns in the same place as the first clone, moves across the screen and hits the rigid body object, there is no collision.
     
  2. enragedmrt

    enragedmrt

    Joined:
    Oct 5, 2009
    Posts:
    95
    Check their positions maybe. Sounds to me like they aren't actually collided then, and since you're working in 2d they could be at different depths at different times for some reason. Just use Debug.Log(transform.position);
    This way you can check when they "should" be colliding if they actually are passing through each other.
    Just a thought.
     
  3. disband85

    disband85

    Joined:
    Dec 7, 2010
    Posts:
    83
    I logged out the positions of both sprites. The positions are the same in both scenarios (when I move the rigid body object and when i don't move it). Yet, collision works when I move it, and doesn't work when I don't move it. It's as though when I move the object, something with it says 'ok i'm ready to run OnTriggerEnter when I collide with something'. Then as soon as it does collide with something, it never runs OnTriggerEvent anymore, until I move the object again. Like the trigger gets turned off when the trigger runs, then turns back on when I move the object.
     
  4. disband85

    disband85

    Joined:
    Dec 7, 2010
    Posts:
    83
    The rigid body object will only collide if i move it AFTER the other object has spawned. I'll make a diagram to try and explain it better...

    Scenario 1:

    1 <----------------- 2

    1 is the rigid body object. 2 spawns, moves across the screen and into 1. Collision doesn't work.

    Scenario 2:

    1 <----------------- 2

    1
    ^ <---------- 2

    v
    1 <------2

    2 spawns. While its moving across the screen, I move 1 up, then move it back down. 1 is now back in the exact same position as in scenario 1. 2 hits 1, and collision works!
     
  5. disband85

    disband85

    Joined:
    Dec 7, 2010
    Posts:
    83
    It might also be worth noting that I have an OnTriggerEnter for both of these objects. In scenario 1, neither of them are called. In scenario 2, both of them are called.
     
  6. disband85

    disband85

    Joined:
    Dec 7, 2010
    Posts:
    83
    Fixed the problem. I added a rigid body to the other object. Kicking myself that I didn't try that earlier >.<