Unity 2D clickable game objects and navigation/pathfinding

109 Views Asked by At

I am new to Unity and trying to make a simple 2D game with three objects that move with different speeds along roads. Player's job is to click on the object and then on the roads the object should follow. If there is a risk of collision the player needs to click on the object again and choose a different route. I have made the objects move and made both the objects and roads clickable, but what sort of mechanism do I need to use to make the objects follow the roads I click? I have watched plenty Youtube-videos about NavMesh, pathfinding, waypoints, splines... but nothing fits my problem.

I would be grateful for any help.

Here is a simplified image of the game: enter image description here

The idea is to choose the route, here for example for the green object: enter image description here

I have made the objects move:

void FixedUpdate()
    {
        transform.Translate(0, Time.deltaTime * speed, 0);
    }

And made the objects and roads clickable:

public void OnPointerDown(PointerEventData eventData)
    {
        Debug.Log("Down");
    }
    public void OnPointerClick(PointerEventData eventData)
    {
        Debug.Log("Click");
    }
    public void OnPointerEnter(PointerEventData eventData)
    {
        Debug.Log("Enter");
    }
    public void OnPointerExit(PointerEventData eventData)
    {
        Debug.Log("Exit");
    }
1

There are 1 best solutions below

0
Peyman Ansary On

you can use dotween or any other tween packages in asset store. and also you can search about "bezier path" How to make game objects follow a path in order in Unity along a bezier curve