- Home /
What is the most efficient way to create a "walkable"area for 2D games?
Hi everyone,
This is just a theoretical question but, I am thinking of starting a 2D game and would love to get your input on what is the best solution (performance-wise) to define walkable areas (basically the are in which a player may move a character and where agents move) for a 2D game like Sword & Sorcery or any other second-person perspective games.
Please consider both point & click movement as well as realtime (X&Y axis).
use colliders and test every cycle before moving;
adultered navmesh 2D hack;
any other...
Thanks
Answer by TTTTTa · May 04, 2016 at 06:36 PM
@deprofundiis One solution is to have a special Tag for the walkable floor areas. Say you have a floor that the player is allowed to walk on. Give it a tag named "Walkable". Then your script is generating Raycasts downward to detect the tag of the current floor. If the floor below the player does not contain the tag "Walkable", then the player is not allowed to move there, IF the floor below the player does contain "Walkable", then the player can move to that location.
True, but is it the most efficient way? Will it cause issues if I decide to port it to mobile because of crappy processors?
It could very well be the most efficient way. Doing one raycast per frame is not going to have any noticeable effect of performance. The Unity character controller does at least one per frame to even deter$$anonymous$$e if the character isGrounded anyways.
Thanks for your input. $$anonymous$$uch appreciated.
Answer by FortisVenaliter · May 04, 2016 at 05:19 PM
In my experience, when Unity's NavMesh won't cut it, Node- or Grid-based A* algorithms tend to be the best balance between ease of setup and final performance. Wikipedia actually has a really good article on A*, complete with pseudocode.
Thank you so much for your answer.
What if I want to do realtime movement, i.e. using X & Y axis input? What do you think would be the best solution?
Well, if you're receiving input, why do you need pathfinding?
Not pathfinding per se but I do need to detect valid movement input.
In this example, I don't want my character to fly all over the screen. If it is on the top of the mountain I want to limit that movement to the top area only.
Your answer
Follow this Question
Related Questions
Why Rigidbody 2d doesnt stop sliding? 2 Answers
Changing only vertical speed on NavMeshAgent? 0 Answers
Physic function doesn't work in different resoultions 0 Answers
Move 2D ground up and down 1 Answer
Player stops moving upon collision 0 Answers