- Home /
Implementing 2D Sprite Collisions
I'm currently working on a 2d sprite based game (using spritemanager 2) with an overworld similar to something like Zelda: Link to the past. My plan is to represent the world in layers, with the deepest layer being a large sprite for the background. I'll then have a GameObject layer (with objects that may or may not have a sprite component) that will consist largely of collison boxes for interaction and scripting.
To prototype out this apporoach, I have a background sprite at my deepest z layer. The texture is a simple path through some grass. To prevent the player from leaving the path, I have a couple of GameObjects with collision boxes in my object layer that cover the grass representing walls, that are set to trigger. These invisible walls are static collision areas that should not move, representing areas the player shouldn't be able to reach. Finally, I have a GameObject also on the object layer with a sprite, rigidbody set to kinematic, and a collision box, that represents my player sprite. I'm moving this player simply by translating a given integer amount each update if a particular key is held down.
What I'm trying to do is script in a simple mechanism that will stop the player from moving if they run into the wall, and will prevent them from moving if the direction is held down.
Currently, I have a public method on my player script that translates the exact opposite of what the last movement translation one. This is called from the wall's OnTriggerEnter method. It effectively just bounces my player off the wall, and just feels really broken.
I'm looking for advice on how to implement an algorithm that will just cleanly stop my sprite in its tracks, and keep his collision box just outside the wall if the player continues to move in the direction of the wall. You know,exactly how you would expect this to behave as a player.
Also, would love to hear any comments if there are any obvious flaws to this overall approach, given what I'm trying to make. Thanks for helping a unity newb!
Answer by kmeboe · Sep 27, 2012 at 10:25 PM
I'm somewhat of a noob when it comes to Unity physics. However, reading the page for Rigidbody.isKinematic (http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody-isKinematic.html), it looks like rigidbody objects with isKinematic set will ignore all of the physics interactions that you desire.
Have you tried making your character's rigidbody not kinematic? This may solve your problem. Is there some reason you want to use a kinematic rigidbody?