Search Unity

2D Navmesh sprite and mesh renderer

Discussion in 'Navigation' started by warrenbrandt, Apr 15, 2020.

  1. warrenbrandt

    warrenbrandt

    Joined:
    Mar 3, 2018
    Posts:
    413
    Hi guys

    trying to get my 2d astronaut walking on a platform

    Nav mesh work using mesh renderers and i have a sprite. I cant add my object in navmesh for the character to walk on. If i try to add a mesh renderer to my sprite i get an error. "cant add mesh reneder as it conflicts with sprite renderer component."

    So i removed the sprite renderer and added a mesh renderer but i cant see any place to put a image.

    I then saw that you need a mesh filter and a mesh renderer to work
    so i added both, but still cant see where to manipulate graphics.

    is mesh only for models? how can i accomplish what i need to?
     
  2. cr8vdev

    cr8vdev

    Joined:
    Apr 4, 2020
    Posts:
    6
    Here is a 'quick-and-dirty' tutorial on how to get NavMesh working for 2D RPG games:


    This isn't a full tutorial though. I recorded this for my own personal documentation purposes.

    Is your game a sidescroller, or is it a top-down RPG?
     
  3. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    359
  4. warrenbrandt

    warrenbrandt

    Joined:
    Mar 3, 2018
    Posts:
    413
    Thanks guys very helpful

    its side view lunar lander type game

    im trying to now have a passenger stand and then walk around, then come to you once you have landed, similar to how choplifter NPCs work...did you ever play choplifter in the 80s?

    Also how do you use that package?

    will it stop unity building my android project, its what im finding so many assets from the store cause problems with android builds.


    thats a fantastic idea to keep track of doing things, think im going to start making my own tutorials too, please share as many as you can with me...please!
     
    Last edited: Apr 15, 2020
  5. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    359
    @warrenbrandt

    I know choplifter, but in that game ground is completely flat and does not require NavMesh system at all.
    Most side-view and 3d navigation games just use physics raycast for navigation.

    So just ray csat your chopper/lunar module to see is it reachable, or better, compare Y values first, and than ray cast for obstacles. Also you can use Observer pattern to just notify about landing. Hard to tell what exactly you want to achieve.

    System for platformers can look like this
    upload_2020-4-15_21-48-29.png
     
  6. warrenbrandt

    warrenbrandt

    Joined:
    Mar 3, 2018
    Posts:
    413
    im just trying to get the npc to walk left and right using rays,

    but im battling, he just keeps walking right and not turning around

    here is the script

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class LeftRightPatrol : MonoBehaviour
    7. {
    8.     public float speed;
    9.     //public float distance;
    10.  
    11.     private bool movingRight = true;
    12.  
    13.     public Transform groundDetection;
    14.  
    15.     private void Update()
    16.     {
    17.         transform.Translate(Vector2.right * speed * Time.deltaTime);
    18.  
    19.         RaycastHit2D groundInfo = Physics2D.Raycast(groundDetection.position, Vector2.down, 2f);
    20.         if (groundInfo.collider == false)
    21.         {
    22.             if (movingRight == true)
    23.             {
    24.                 transform.eulerAngles = new Vector3(0, -180, 0);
    25.                 movingRight = false;
    26.             }
    27.             else
    28.             {
    29.                 transform.eulerAngles = new Vector3(0, 0, 0);
    30.                 movingRight = true;
    31.             }
    32.         }
    33.     }
    34. }
     
  7. warrenbrandt

    warrenbrandt

    Joined:
    Mar 3, 2018
    Posts:
    413
    got it sorted, had to be a platform, need to make it along the ground? but how?
     
  8. warrenbrandt

    warrenbrandt

    Joined:
    Mar 3, 2018
    Posts:
    413
    heres a bit of a demo
    the end is a bit heavy, def 12pg lol

     
  9. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    359
    It depends what are you trying to achieve. You can create "wanderer" script that will accept waypoint (empty gameobject) and radius on how far you can go. So, walk to the right\left till you exceed distance from way point, or edge of a platform.
     
  10. warrenbrandt

    warrenbrandt

    Joined:
    Mar 3, 2018
    Posts:
    413
  11. cr8vdev

    cr8vdev

    Joined:
    Apr 4, 2020
    Posts:
    6
    The videos I create does not have many use cases and aren't full-featured. However, if you want tutorials that are short, to the point and easy to understand then I suggest checking out Brackeys. He has loads of tutorials and also creates tutorials on request (depending on how much popularity that request has).
    https://www.youtube.com/channel/UCYbK_tjZ2OrIZFBvU6CCMiA
     
  12. warrenbrandt

    warrenbrandt

    Joined:
    Mar 3, 2018
    Posts:
    413
    yes i know mr smiley Brackeys, very good, thanks!!!