Search Unity

2D Nav Mesh Agent

Discussion in '2D' started by tarzanno, Apr 30, 2015.

  1. tarzanno

    tarzanno

    Joined:
    Apr 23, 2015
    Posts:
    58
    Is it possible to use a Nav Mesh Agent in a 2D game? For now, my game is in x-y axis, but NavMeshAgent's height is on axis Y, so not in a way my game is. Is there any way to change axes of this component? Maybe there is another way to make it work (because for now, it doen't work at all)?
     
    Last edited: Apr 30, 2015
  2. TomasJ

    TomasJ

    Joined:
    Sep 26, 2010
    Posts:
    256
    Currently NavMesh can't be used on the XY plane (but this feature is coming).
    If you really want, you can rotate sprites to be on the XZ plane and point your camera down.
     
  3. tarzanno

    tarzanno

    Joined:
    Apr 23, 2015
    Posts:
    58
    Thank you for reply!
    Can you tell me where to find how to do this?
    Also, is there something similar to NavMesh, but for 2D enviroment?
     
  4. TomasJ

    TomasJ

    Joined:
    Sep 26, 2010
    Posts:
    256
    I don't know of any 3rd party assets to do this.
    But do check out these videos:


    perhaps that is enough for your project?
     
    tarzanno likes this.
  5. tarzanno

    tarzanno

    Joined:
    Apr 23, 2015
    Posts:
    58
    I think it is perfect, thanks!
     
  6. tarzanno

    tarzanno

    Joined:
    Apr 23, 2015
    Posts:
    58
    For now, I have a problem to change my PlayerMobility.cs script so the Player go horizontally on axis X and vertically on axis Z (previously the vertical movement was on axis Y). I don't know what should I change in a script. Can you help me?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerMobility : MonoBehaviour {
    5.  
    6.     public float speed;
    7.     // Normal Movements Variables
    8.     private float walkSpeed;
    9.     private float curSpeed;
    10.     private float maxSpeed;
    11.     private float sprintSpeed;
    12.    
    13.     private CharacterStat plStat;
    14.     Animator anim;
    15.  
    16.     //chodzenie postaci
    17.     void Start()
    18.     {
    19.         plStat = GetComponent<CharacterStat>();
    20.        
    21.         walkSpeed = (float)(plStat.Speed + (plStat.Agility/5));
    22.         sprintSpeed = walkSpeed + (walkSpeed / 2);
    23.         //koniec chodzenie postaci
    24.  
    25.         anim = GetComponent<Animator> ();
    26.  
    27.     }
    28.     void Update()
    29.     {
    30.         if(Input.GetMouseButtonDown (0)){
    31.             anim.SetTrigger ("atak");
    32.     }
    33.     }
    34.     void FixedUpdate ()
    35.     {
    36.         //ruch w kierunku myszki
    37.         var mousePosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
    38.         Quaternion rot = Quaternion.LookRotation (transform.position - mousePosition,
    39.                                                   Vector3.forward);
    40.         transform.rotation = rot;
    41.         transform.eulerAngles = new Vector3(0,transform.eulerAngles.y,0);
    42.         GetComponent<Rigidbody2D>().angularVelocity = 0;
    43.         //koniec ruchu myszki
    44.  
    45.         //chodzenie postaci wsadem
    46.         curSpeed = walkSpeed;
    47.         maxSpeed = curSpeed;
    48.        
    49.         // Move senteces
    50.         GetComponent<Rigidbody2D>().velocity = new Vector3(Mathf.Lerp(Input.GetAxis("Horizontal")* curSpeed, 0.8f,0),
    51.                                                            Mathf.Lerp(0.8f, 0, Input.GetAxis("Vertical")* curSpeed ));
    52.    
    53.  
    54.     }
    55.  
    56.    
    57.  
    58. }
    59.  
     
  7. Smikis

    Smikis

    Joined:
    May 3, 2015
    Posts:
    7
    Any eta on 2d navMesh ? Rotating to 90 degrees allows nav meshes in 2d or 3d project, but there are gazilion of issues, constantly resetting view if you have "maximise on play", landscape that becomes invisible to camera if it's set to "static" , controls doesn't work, making you switch to 3d objects for that z axis... and no collider navigation , I hope that is coming with 2d nav mesh?