- Home /
Why aren't my 2D sprites colliding properly?
I've looked everywhere on the internet for an answer, but I haven't been able to find one yet, so I'm trying here.
I'm trying to make a very basic game in Unity2D that involves a player sprite being moved via transform.position. The player has a Box Collider 2D, as well as a Rigidbody 2D. I also have a floor sprite as well that just acts as a static box with a Box Collider 2D at the bottom of the screen. The problem is, when the two collide, the player just goes behind the ground without ever being stopped by it. I set up a collision detector in the script, so I know that the game recognizes a collision is happening, but no actual collision occurs.
Here are the inspectors for both objects:
Player: Ground:
Nothing is set to Kinematic, everything is set to 2D, both sprites have their Z coordinates set to 0, the player is moving far too slow to ignore a collider as big as the ground's, and I can't see anything that would make it so they would pass through each other.
I assume the problem is with the player's movement being based on transform.position, but I haven't seen anything online that says that wouldn't work, so I'm stumped at this point. Hopefully someone here can shed some light on the situation. I'll give any extra info I might've forgotten, and many thanks in advance everyone
Answer by BallStimulator · Sep 19, 2020 at 01:40 PM
Alright, so I was messing with random attributes and I managed to find the solution!
Turns out, I just had to un-check the "isTrigger" box for both of the objects. I looked at the Documentation page for the isTrigger box, and apparently it disables all actual collision detection.
Answer by Gustard · Sep 16, 2020 at 08:41 PM
Just because you have the colliders set doesn't mean they will actually do anything in this scenario. You will need to make it so that, on collision, the transform.position method can't move the character forward anymore. This is easiest by setting a speed variable that goes to 0 on collision. If I misunderstood your progress and you have done this already, I would be happy to look over your code and see if I can solve this for you. Just post it in the comments.
Your answer
Follow this Question
Related Questions
how to make a 2d character three-dimensional? 1 Answer
My player is not rotating upwards when it moves upwards. Any advice? 1 Answer
Default Contact Offset Problems 1 Answer
Shooter 2D - Problem with rotation of bullet to face direction (velocity) 2 Answers
Slow Sprite Rotation Toward Movement Direction - Top-Down 2D Game 0 Answers