Home > front end >  Generate a path based on placed shapes and make a Gameobject walk the path
Generate a path based on placed shapes and make a Gameobject walk the path

Time:09-22

I am making a game where the player place shapes to make a path that connects a Starting point to a Destination point and make a wagon walk on that path.

enter image description here

I am stuck on how to make the path and force the wagon respect the switch points even if the Destination point is not in that direction. I tried NavmeshSurface on the path and made the wagon a Navmesh agent, it walks on the path and make it to the destination point but it ignores the switch point. I have no idea how to make this works, please help me with your suggestions and ideas on how to generate a path according to the shapes placed and make the wagon follow that path thank you in advance :)

CodePudding user response:

Step 1: Create 2 new scripts

Step 2: Attach one to the wagon you wish to move. This script will be for pathfinding.

Step 3: Copy-paste this code into the script you attached to the wagon:

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

`public class Pathfinder : MonoBehaviour
{
    WaveConfigSO waveConfig;
    List<Transform> waypoints;
    int waypointIndex = 0;

    void Start()
    {
        waypoints = waveConfig.GetWayPoints();
        transform.position = waypoints[waypointIndex].position;
    }

    void Update()
    {
        FollowPath();
    }

    void FollowPath()
    {
        if (waypointIndex < waypoints.Count)
        {
            Vector3 targetPosition = waypoints[waypointIndex].position;
            float delta = waveConfig.GetMoveSpeed() * Time.deltaTime;
            transform.position = Vector3.MoveTowards(transform.position, targetPosition, delta);
            if (transform.position == targetPosition)
            {
                waypointIndex  ;
            }
        }
    }
}

Step 4: Copy-paste this code into the other script:

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

[CreateAssetMenu(menuName = "Path Config", fileName = "New Path Config")]
public class WaveConfigSO : ScriptableObject
{
    [SerializeField] Transform pathPrefab;
    [SerializeField] float moveSpeed = 5f;

    public List<Transform> GetWayPoints()
    {
        List<Transform> waypoints = new List<Transform>();
        foreach(Transform child in pathPrefab)
        {
            waypoints.Add(child);
        }
        return waypoints;
    }


    public float GetMoveSpeed()
    {
        return moveSpeed;
    } 
}

Step 4: Make an empty gameObject in your hierarchy. This gameObject will hold the waypoints for your path

Step 5: Make another empty game object that is inside the previous one. Name it "Waypoint (0)". The parentheses are important so that when you duplicate the waypoints the index automatically gets incremented.

Step 6: Change its icon so that you can visualize the waypoints enter image description here

Step 7: Duplicate the waypoint as many times as you wish.

Step 8: Arrange the waypoints for your wagon to follow on the track

Step 9: Prefab the path and create a new scriptable object. Set the speed to whatever you want and attach the path prefab to the variable slot on the scriptable object.

Step 10: Go to your wagon and set up the scriptable object as the variable needed for the pathfinding script that the wagon has.

Step 11: Enjoy

So I did a couple of changes relative to my case, which means there's a good chance this setup won't work from the get-go. If that happens just answer here with the problem and I will help you debug it if needed.

One other thing: this setup is purely for testing purposes. As for giving control to the player, you will obviously have to make some other changes. For example, when a new tile track is placed, instantiate a new waypoint for the path right on top of it. If you have trouble with that too, let me know, I might be able to help on that case as well.

Vlad

  • Related