- Home /
I want collision boxes to be precise without overlapping each other
I want the player's movement to be very precious, but without collision boxes overlapping each other. Because with overlapping, the player wouldn't be able to move forward (I have set it so that player has a velocity of 0 if they ever hit a collision box). Keep in mind, my player moves like how tiles in 2048 (game) works. Any advice are appreciated. Thanks in advance! :)
Can you go into a little more detail? Hard to understand what you're even asking.
Hi there! So basically, both the player and the block has a collision box with a scale of 1. Their sizes are both 1 unit. However, when I play, they "overlap" each other, making the player unable to move to the desired position. Looking at the picture, the collision box is preventing the player from moving to the right. I want to be able to scale the collision box down to 0.8, but then the player (which moves like a tile from 2048), will have their body stuck into one of the block, and that's not ideal.
Answer by Major · Mar 07, 2019 at 10:54 PM
You aren't going to get that level of precision with the physics engine as is. There has to be some level of overlap (however small) because of the way collision detection and collision response is handled. Furthermore, relying on floating point numbers with distances that small is not a great idea, as you will probably get some precision issues anyways.
However, if your player is supposed to move in a fashion very similar to 2048 tiles, then you don't need to use the physics engine in the first place. In fact, it would be over kill to do so. You would be better off storing your level data in a 2D array of, say integers where the value at some position in the array tells you what kind of tile is at the position. When you want to move the player, you can check for tiles that are impenetrable in the direction of travel. If a tile exists that the player should not be able to move through, then you do the entirely obvious thing of... don't move them.
With that being said, you would also need to store the player position as a pair of integers so that you can quickly index the level tile data to check for potential collisions.
Obviously this is more work than just dropping sprites into the scene, but it will get you the level of precision you want in your moment system, and separate you from Unity's physics engine and allow you to implement your own behaviors.
Sorry for the late reply, but thank you for the advice! Appreciate it!
Your answer
Follow this Question
Related Questions
BoxCollider get stuck when dragging over the border of two BoxColliders 1 Answer
(Steam VR / Vive) Rigidbody moving in only one direction after collision 1 Answer
Why is the relativeVelocity of a Collision always zero in OnCollisionEnter and OnCollisionStay? 0 Answers
Big issue with colliders 1 Answer
Resume movement of instanced object relative to the original after instantiation. 2 Answers