Forum rules - please read before posting.

Direct movement within navmesh in 2D games

I'm playing around with direct movement for porting my (2D) game to consoles. Controlling the player works, but getting him to stay within the navmesh is not happening.

The manual suggests that it should be doable, but when I change pathfinding method in scene settings (as suggested here https://www.adventurecreator.org/tutorials/unity-navigation-pathfinding) to Unity Navigation, AC tells me that "This method is not compatible with 'Unity 2D' mode".

Sticking 2D colliders outside all the navmeshes doesn't feel like a solution as I got tons of them in the game I'm porting. Any alternative way to accomplish this?

Thanks

Comments

  • Unity Navigation pathfinding is only available for 3D scenes, and so the use of the baked NavMesh to limit direct-control is only avaiable for 3D games. I shall make this more clear in the Manual.

    In 2D, you might be able to get by with checking if the character is on the NavMesh each frame, and move them to the closest point on it if not, e.g:

    using UnityEngine;
    using UnityEditor;
    using AC;
    
    public class StickTo2DNavMesh : MonoBehaviour
    {
    
        private Vector3 lastPositionOnNavMesh;
    
        private void LateUpdate ()
        {
            Vector3 position = transform.position;
            var poly = KickStarter.sceneSettings.navMesh.GetComponent<PolygonCollider2D> ();
            position = poly.ClosestPoint (position);
    
            if (position != transform.position)
            {
                GetComponent<AC.Char> ().Teleport (position);
            }
        }
    
    }
    
  • Cool, I might use that technique for a future game👍

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.