Search Unity

Move GameObject along a given path

Discussion in '2D' started by Kurdiez, Feb 7, 2017.

  1. Kurdiez

    Kurdiez

    Joined:
    Feb 6, 2017
    Posts:
    72
    I am trying to make a world map that looks and behaves similar to the game Braveland.



    To see how the character navigates around, here is a short video,



    As you can see, when you click on a node, character finds its path to it and walks along the path. I can draw up my own map, save it in PNG and import it into Unity but I am not sure where to go from there. Do I lay on top of the map image, some game objects as the nodes to listen to click events? How do I force my character to walk only on the paths of the map?
     
    smock_74 likes this.
  2. theANMATOR2b

    theANMATOR2b

    Joined:
    Jul 12, 2014
    Posts:
    7,790
    I've not had to solve this mechanic yet, although I will in the near future. Have you looked into Navmesh? It might be what you are looking for. Also there are some light weight point to point nav agents in the asset store that might be helpful.
     
  3. Kurdiez

    Kurdiez

    Joined:
    Feb 6, 2017
    Posts:
    72
    I followed this tutorial here to achieve point-to-point moving.



    Here is what I have done in Unity.

    upload_2017-2-8_4-53-3.png

    Here is the key script I wrote based on the Youtube tutorial above.

    upload_2017-2-8_4-54-55.png

    The underlined red is where the animation happens. However this code results in something like the following animation where the character pauses in between the dots.

    demo (0).gif

    I want the character to follow this path in one smooth motion without any intermittent pauses.
    Could somebody help?
     
    BunTra90K and Bakanovskiy95 like this.
  4. RockyWallbanger

    RockyWallbanger

    Joined:
    Mar 16, 2014
    Posts:
    85
    The reason you're getting that segmented motion is because you're lerping from your position which changes every Update. Think of it like this; you want your character to move some fraction of the distance between point A and point B a given number of times until it reaches the destination. But you're actually telling it, move some fraction of a distance from where it currently is to point B. What you need to do is create another class level Vector2 variable called startPosition or something like that, then in your CheckNode() function add...
    Code (CSharp):
    1. startPosition = Player.transform.position;
    And in Update, change your lerp call to...
    Code (CSharp):
    1. Player.transform.position = Vector3.Lerp(startPosition, CurrentPositionHolder, Timer);
    I haven't tested that but it should work.
     
  5. Kurdiez

    Kurdiez

    Joined:
    Feb 6, 2017
    Posts:
    72
    Fantastic. Thanks @RockyWallbanger . With this I can construct the map no problem now. Thank you all.

    Just one last question, you see how my character's pivot is in the middle of the sprite, is there a way to have it at the bottom where the foot is?

    demo (0).gif
     
    Bakanovskiy95 likes this.
  6. theANMATOR2b

    theANMATOR2b

    Joined:
    Jul 12, 2014
    Posts:
    7,790
    Yes - several ways - change the pivot of the sprite either in Unity or authoring tool, or parent the character to a game object placed where you want the pivot to be.

    Some documentation research - might help going forward. ;)
     
    Kurdiez likes this.
  7. Kurdiez

    Kurdiez

    Joined:
    Feb 6, 2017
    Posts:
    72
    @theANMATOR2b Thanks. Found out I can do that in the Sprite Editor right in Unity. Thanks for the tip.
     
    theANMATOR2b likes this.
  8. NileshS

    NileshS

    Joined:
    Apr 1, 2015
    Posts:
    3
    The above code works fine.. here i pasted my code for reference.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class PathFollower : MonoBehaviour {

    public GameObject[] PathNode;
    public GameObject Player;
    public float MoveSpeed;
    float Timer;
    static Vector3 CurrentPositionHolder;
    int CurrentNode;
    private Vector2 startPosition;


    // Use this for initialization
    void Start () {
    //PathNode = GetComponentInChildren<>();
    CheckNode ();
    }

    void CheckNode(){
    Timer = 0;
    startPosition = Player.transform.position;
    CurrentPositionHolder = PathNode[CurrentNode].transform.position;
    }

    // Update is called once per frame
    void Update () {

    Timer += Time.deltaTime * MoveSpeed;

    if (Player.transform.position != CurrentPositionHolder) {

    Player.transform.position = Vector3.Lerp (startPosition, CurrentPositionHolder, Timer);
    }
    else{

    if(CurrentNode <PathNode.Length -1)
    {
    CurrentNode++;
    CheckNode ();
    }
    }
    }
    }

    upload_2019-1-5_13-53-19.png upload_2019-1-5_13-53-19.png upload_2019-1-5_13-53-19.png upload_2019-1-5_13-53-19.png
     
  9. Mikzalianz

    Mikzalianz

    Joined:
    Mar 27, 2017
    Posts:
    1
    Code (CSharp):
    1. public Transform nodes;
    2.  
    3. void Awake()
    4. {
    5. PathNode=new GameObject[nodes.childCount];
    6. for(int i=0; i<nodes.childCount; i++)
    7. {
    8. PathNode[i] = nodes.GetChild(i).gameObject;
    9. }
    10. }
    Or you can paste this code to avoid Drag and drop the nodes into inspector
     
    MuhammadZekab likes this.
  10. GamerFawn

    GamerFawn

    Joined:
    Sep 6, 2020
    Posts:
    2
    Sebastian Lague created a cool Path Creator tool:


    Might be helpful in this scenario?
     
  11. Dudusstar

    Dudusstar

    Joined:
    Nov 19, 2020
    Posts:
    59
    Hi! Do you know how can I move my character inside this type of path? I mean, I don't want him to follow it from start to end. I want to make like a path grid and move through a ball .