Hello Guest

Author Topic: Sprite Collision Detection without physics?  (Read 6177 times)

LordYabo

  • Newbie
  • *
  • Posts: 17
    • View Profile
Sprite Collision Detection without physics?
« on: July 05, 2013, 07:56:58 pm »
Hi,

I have a point and click adventure game.  I have a main character and some doors on the screen.
I want to know when the character "collides" with a door object so I can programatically do something.

I added a "Box Trimmed" collider to the door object.  Under sprite settings put collider depth to 1.
I added a "Box Trimmed" collider to my player object.  Under sprite settings put collider depth to 1.
All objects have 0 z position.

In the editor, both objects have box colliders with a Z size of 2 with a nice green outline on them.
I turned on Is Trigger

In my player object script I added:
void OnTriggerEnter(Collider other)
   {
      Debug.Log("OnTriggerEnter " + other.name);
   }

I move my player through Transform.position updates in a coroutine.  No physics/forces/etc.

The trigger is never called. My character can walk all over the door object.

Am I missing something?  Do I have to use rigid bodies for colliders to work?

Edit:
After trying many things (raycasting) I added rigid bodies to my door and player objects. 
At first they started falling (which I don't want), then I discovered IsKinematic = false which tells the physics engine not to touch my object.  Now I have my triggers working with the colliders without physics, which is what I was trying to do.

It sure would have been great if:
a) The 2d toolkit documentation/tutorial had told me I had to add a rigid body to my sprite that I added a box collider too
b) The 2d toolkit when adding a box colllider saw I didn't have a rigid body and added it for me. 

Maybe every other unity developer knows to do this, but I didn't.  :'(
« Last Edit: July 05, 2013, 10:09:56 pm by LordYabo »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Sprite Collision Detection without physics?
« Reply #1 on: July 05, 2013, 10:51:43 pm »
Those are more or less rules for physics in Unity. I could add to the documentation, but where do you stop? There are a lot of fundemental things in Unity which you really need to understand before you can make a game in it, be it 2d or 3d. This is one specific use case for moving something, but you could use the character controller, add forces, etc.

FWIW, I actually do agree with you, this is actually covered in our big tutorial.

LordYabo

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Sprite Collision Detection without physics?
« Reply #2 on: July 06, 2013, 02:38:58 am »
Where is the big tutorial?  I've looked around the documentation and haven't seen one.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Sprite Collision Detection without physics?
« Reply #3 on: July 06, 2013, 11:50:55 am »
Apologies it was super late last night and I actually missed out some crucial info in the last reply. What I meant was, we have a comprehensive detailed list in our NEW, YET UNRELEASED tutorial. It will be released soon, the last things I was waiting on is all done now.