Hello Guest

Author Topic: Collision and collider problems (triggers)  (Read 10985 times)

eshan.mathur

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 14
    • View Profile
Collision and collider problems (triggers)
« on: October 14, 2012, 04:35:49 am »
Hey,

I'm trying to detect when two sprites are colliding, and decided to use triggers as this was recommended in the Unity documentation as a very simple way to do collisions. I couldn't figure out how to make 2D Toolkit generated (box-trimmed) colliders to be triggers, so I simply switched the sprite colliders to "Unset" and committed the sprite collection.

I then dragged an instance of my main character (the player's car) sprite into the world and gave it a box trigger collider and gave it a rigidbody (and checked IsKinematic). I also have a prefab of a tk2dsprite called generic_car that I generate multiples of using Instantiate(). I placed a box trigger collider on that prefab. When I run the game, everything looks good (almost).

I placed a script on the main character sprite and put an onTriggerEnter(Collider other) function in there with a simple Debug.Log("Collision!") call.

Issue 1: The debug log never gets called, so collisions aren't working.
Issue 2: Something funky is going on because when I set the z-value of the main character sprite's box collider to say, 100, it shows up all nice and thick - until I press play. After running the game, it switches back to 0.2 and I cannot say why. I thought it was because 2D Toolkit was overwriting the collider component but I double checked my SpriteCollection and every single sprite has it's collider to 'Unset'.

Could use some help figuring out what's going on. AFAIK the Unity collision matrix says a rigidbody trigger collider (kinematic or not) will collide with any kind of box collider.

Thanks!
« Last Edit: October 14, 2012, 04:38:52 am by eshan.mathur »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Collision and collider problems (triggers)
« Reply #1 on: October 14, 2012, 05:19:50 pm »
To set up a trigger, set up a sprite as you'd normally do with a collider, and then create an instance of it, and tick trigger. This setting is remembered when the sprite is updated.

1. Are you moving the sprites which don't have rigidbodies attached to them? I've had bizzare things happen when colliders without rigidbodies are moved at runtime.

2. I'm fairly certain the z depth of box collider is set up by tk2d. You can set up depth in sprite collection settings > Collider depth, so try changing that to something different to see if the number changes (it should). I'm pretty sure its overwriting your setting on startup and the easiest way to track this down is to set a breakpoint in tk2dBaseSprite.UpdateCollider and see what's happening in there.

eshan.mathur

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Collision and collider problems (triggers)
« Reply #2 on: October 14, 2012, 11:29:37 pm »
Giving the generic car prefab a rigidbody (and kinematic) didn't generate collisions. I also tried removing the movement from the generic cars but there was still no trigger message sent.

I set the collider depth to 100 in the SpriteCollection settings and committed, but still found all the box colliders to be a depth of 0.2. It's really strange. I set a breakpoint in tk2dBaseSprite - by the time it hit UpdateCollider, it was already a depth of 0.2. I stepped through CreateCollider out of curiosity also. When I delete the collider manually and press play, it creates a collider as I would expect but then UpdateCollider gives it a z of 0.2, despite the collider depth setting.

I could actually care less about the depth of the collider, as long as collisions worked (especially since the main character sprite and the generic sprites are all on the same Z). But they don't :(

Probably not this but is it possible that I've placed my onTriggerEnter function in the wrong place? It shouldn't be... it's in a script placed on the main character sprite.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Collision and collider problems (triggers)
« Reply #3 on: October 14, 2012, 11:55:44 pm »
Just checking, you spelt the function OnTriggerEnter right?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Collision and collider problems (triggers)
« Reply #4 on: October 14, 2012, 11:56:57 pm »
Just tested, it works both kinematic - collideronly, and kinematic - kinematic.

eshan.mathur

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Collision and collider problems (triggers)
« Reply #5 on: October 15, 2012, 12:11:28 am »
God damn it. OnTriggerEnter did it. Strangely it only triggers when I move the main character sprite, not when the generic ones hit it on their own, but I'll look into that.

Any idea why the collider depth isn't being properly set?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Collision and collider problems (triggers)
« Reply #6 on: October 15, 2012, 12:21:54 am »
No, I wasn't seeing the same behaviour here (with collider type set to unset, it wasn't bothering what was set up manually). What version are you on?

eshan.mathur

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Collision and collider problems (triggers)
« Reply #7 on: October 15, 2012, 12:25:28 am »
1.76 Final + patch 1.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Collision and collider problems (triggers)
« Reply #8 on: October 15, 2012, 09:40:49 am »
I looked at the VCS history and I couldn't find anything suspect, none of them has changed in recent past. Bizzare.
I'm preparing 1.80 for release now - I'll look into this after that has gone out...