Hello Guest

Author Topic: Collisions  (Read 8605 times)

bigtunacan

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 15
    • View Profile
Collisions
« on: April 21, 2012, 02:03:17 pm »
I'm a bit of a noob to Unity as well as the 2D Toolkit.  I'm trying to do collisions between animations to animations, but I just can't figure out how to script this.  I've created colliders in the editor, but I can't see how to access and use them in the script.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Collisions
« Reply #1 on: April 21, 2012, 05:49:38 pm »
What kind of collisions are you after?
If you just need to find out of two objects are colliding / intersecting with one another - set both of them to trigger, and use OnTriggerEnter to get this information. Note that you'll have to give at least one of them a rigidbody (and also set IsKinematic to true if you're moving them from script)

bigtunacan

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Collisions
« Reply #2 on: April 21, 2012, 06:50:41 pm »
I just want to check for a collision between the player and enemies, pretty basic.  So if I give my character a rigid body, but not to anything else; then I can do character to enemy collisions, but not enemy to enemy?  Is that right?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Collisions
« Reply #3 on: April 21, 2012, 08:24:12 pm »
Yup. You can have the OnTriggerEnter function on either the player or the enemy - pros and cons to both.

bigtunacan

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Collisions
« Reply #4 on: April 25, 2012, 06:47:05 pm »
Not sure what I'm doing wrong, but I still can't get this working :(

I have my main character which is an Animated Sprite, and I have added to this a Rigidbody.  I have added a Box Collider to my main character.  I have checked Is Trigger and Is Kinematic.

My enemy is a prefab that I create in the script.  Within the prefab I have added a Box Collider.

Just as a simple test to see if things were working I added this function into the script associated with my main character.

Code: [Select]
void OnTriggerEnter(Collider other){
Destroy(other.gameObject);
}

It looks like the function is never being called, even though I clearly see the objects intersection at playback.  :(

bigtunacan

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Collisions
« Reply #5 on: April 26, 2012, 02:00:00 am »
Ok; there were 2 issues I was having.  One was for my test my character wasn't moving; the OnTrigger function is only called if the sprites position changes.  Second issue I ran into was that the z depth was causing some sprites to not collide since the mesh thickness is only .2.  It would be nice if the collider thickness could be changed.  I like to draw my sprites at different depths even though I intend for them to collide so that I can control what draws in front/back.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Collisions
« Reply #6 on: April 26, 2012, 08:38:47 am »
You can increase the zdepth of the colliders generated by changing it in the sprite collection editor, settings panel.

Marble

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Collisions
« Reply #7 on: May 05, 2012, 11:37:07 pm »
I don't know how good or bad a practice this is, but since kinematic rigidbodies seem to only trigger when their transforms are updated (even if not "sleeping" and the geometry is 'moving' as part of the animation), you can just do something like:

Code: [Select]
if( spriteAnimation.isPlaying() ) transform.position = transform.position;

 :P