Hello Guest

Author Topic: Can't seem to get collisions to work!  (Read 6625 times)

scottstephan

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 6
    • View Profile
Can't seem to get collisions to work!
« on: November 05, 2012, 01:41:43 am »
Hola! 2DTK newbie here, so apologies if this is a goofy question. Here's my issue: I can't seem to get anything to collide!

I have a player sprite with a box collider (Set from the sprite collection as "box trimmed) and a water sprite/tile (Set as "box trimmed" from the collection) that I'd like to collide, essentially preventing the sprite from moving past it. However, the player sprite seems to just pass over/through the tile sprite. It's almost as if they're on different layers.

If I add a rigidbody to the player object, lock its rotation and Z-Pos and add gravity, it'll collide with the top of the water sprite/tile just fine, but seemingly not the sides.

What's my issue here? I can't seem to get 2 2D sprites, locked on the same Z, to collide. I feel like I must have weird misunderstanding of how collisions in 2DTK work, but can't seem to find anything in the docs that indicate this.

scottstephan

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Can't seem to get collisions to work!
« Reply #1 on: November 05, 2012, 03:00:42 am »
So, I've been mucking with this for a few hours and here what I've found: Assigning the colliders from the sprite collection seems to generate colliders with low Z-depths. Only by manually assigning a box collider component and then increasing the Z-Depth (From something like 0.2 to 15) was I able to get my objects to register a collision.

However, I can't believe I'm the only person having this issue. YouTube tutorials make no mention of it and if you assign colliders automatically from the sprite collection, you don't have the option of adjusting Z-Depth, making me think that it must be something else.

As a side note: Even now that I can collide objects, colliding seems to "bounce" the rigidbody object (The player). This seems like a Unity concern, but can anyone shed some light on it?

scottstephan

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Can't seem to get collisions to work!
« Reply #2 on: November 05, 2012, 03:25:12 am »
Also, this solution only seems to block objects from along the topmost edge- In many cases I can still enter from the side. I have no idea what's going wrong and I've been trying dozens of permutations for hours. I feel like I have to be overlooking something very, very basic. Clues?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Can't seem to get collisions to work!
« Reply #3 on: November 05, 2012, 12:35:21 pm »
If you're using a tk2dCamera / or not, you can increase z depth in the sprite collection editor.
http://unikronsoftware.com/2dtoolkit/doc/reference/sprite_collection.html
"Collider depth" is what you want.

How are you moving your player collider? You should always use AddForce to move a rigidbody, otherwise you'll be doing very very bad things to the physics engine - its the equivalent of the player teleporting otherwise.

Also, most people tend to use a Unity character controller - its a lot more stable and controllable.

scottstephan

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Can't seem to get collisions to work!
« Reply #4 on: November 06, 2012, 04:25:31 am »
Is there a recommended controller for 2D work? That's where I seem to be, ahem, hitting the wall.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Can't seem to get collisions to work!
« Reply #5 on: November 06, 2012, 02:37:30 pm »
The easiest thing to use is the "Character controller" component built in to Unity.
http://unity3d.com/support/resources/tutorials/2d-gameplay-tutorial

Thats a good start and the concepts apply directly.