- Home /
2D C# isometric Tile-based movement and collision problem
Hi, I am having a little bit of trouble trying to make my character collide with other sprites like walls or simply stopping it from going outside the tilemap tiles whilst allowing for multiple floor levels.
- How should I go about doing so?
The floor level part I already have a good idea how I will manage it, but I have already tried fixing the collision part over the last few days and so far this is the only thing stopping me from advancing my project further.
The movement itself works great and without any problem so far. I simply cannot figure out how to make the collision work.
- What I tried already:
Having the player use a Rigidbody2D with gravity set to 0 and a tilemap collider on the tiles. -- Worked-ish, but was far from what I would want. For some reason going into a wall makes the character bounce back since the position the character tries to move onto would end up in a collision loop. I did combine the collisions as to reduce the performance load and manage to make the collision boxes be actual boxes and not weirdly formed and it still had that problem.
Detect if there is a Tile in the direction the Player Character is moving towards. -- I could not figure out how to detect the specific tile through the C# script itself.
- The Way I have my script setup:
(Excluding the Collisions since I removed the failed attempt I had to keep my code cleaner. But put simply I tried to get the tile and if its type was null I would not allow movement, but I cannot figure out how to get said specific tile type at the direction position)
I do have the tilemap put into a variable if this helps out in anyway to get the specific tile type. And since I cannot input a float into the method it has to get the tile, I have no clue how to do it.*
-- The way I send the movement if (Input.GetKeyDown(KeyCode.W)) { direction += new Vector3(1.0f, 0.5f); }
-- The way I make the character move MoveCharacter(Vector3 dest) transform.position = Vector3.MoveTowards(transform.position, dest, movementSpeed * Time.deltaTime);
I would greatly appreciate any help towards making this collision work, thank you in advance for your answers.
Update to the question: I have managed to make the up and left collision work, but for some reason it does not work when going down or right. I am currently using the 2D Raycast method.
Answer by unity_ek98vnTRplGj8Q · Feb 02, 2021 at 11:42 PM
Use Rigidbody2D.MovePosition() instead of setting the transform.position directly. If you set transform.position directly it will move your character without regard to any colliders or physics calculations, then the physics system will try to fight that position afterwards which results in the character bounce back. Rigidbody.MovePosition checks colliders while it moves. You may also want to look into the CharacterController script which can help solve some of the weirdness of rigidbodies.
Huge thank you for the suggestion! Having an actual contact point to deter$$anonymous$$e if the character can move towards that point or not helps a lot. And as you said, the physics works super great using Rigidbody2D.$$anonymous$$ovePosition()
instead. You are an absolute legend <3