Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to create endless path?

Discussion in 'Scripting' started by polan31, Oct 20, 2018.

  1. polan31

    polan31

    Joined:
    Nov 20, 2017
    Posts:
    149
    UNITY 2D C#

    Hi everyone .

    I have scripts that allow me to create and modify path follow.

    However, I have a problem.

    I would like to create a path like in the sling drift game.

    The idea is that it would be infinite and change the direction, for example, down, up, right, left, etc.

    How to do it?

    My scripts:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6. public class PathCreator : MonoBehaviour {
    7.  
    8.     [HideInInspector]
    9.     public Path path;
    10.  
    11.  
    12.     public Color anchorCol = Color.red;
    13.     public Color controlCol = Color.white;
    14.     public Color segmentCol = Color.green;
    15.     public Color selectedSegmentCol = Color.yellow;
    16.     public float anchorDiameter = .1f;
    17.     public float controlDiameter = .075f;
    18.     public bool displayControlPoints = true;
    19.  
    20.  
    21.  
    22.     public void CreatePath()
    23.     {
    24.         path = new Path(transform.position);
    25.  
    26.     }
    27.  
    28.     void Reset()
    29.     {
    30.         CreatePath();
    31.     }
    32. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.Linq;
    5.  
    6. public class PathPlacer : MonoBehaviour {
    7.  
    8.     public float spacing = .1f;
    9.     public float resolution = 1;
    10.     public GameObject someObject;
    11.  
    12.     bool objectCreated = false;
    13.  
    14.  
    15.     void Start () {
    16.         Vector2[] points = FindObjectOfType<PathCreator>().path.CalculateEvenlySpacedPoints(spacing, resolution);
    17.  
    18.  
    19.  
    20.         foreach (Vector2 p in points)
    21.         {
    22.             GameObject g = GameObject.CreatePrimitive(PrimitiveType.Sphere);
    23.             objectCreated = true;
    24.             Debug.Log ("Created");
    25.  
    26.             g.transform.parent = someObject.transform;
    27.             g.transform.position = p;
    28.             g.transform.localScale = Vector3.one * spacing * .5f;
    29.  
    30.  
    31.     }
    32.  
    33. }
    34. }
     
  2. SVKsuli

    SVKsuli

    Joined:
    Mar 23, 2014
    Posts:
    63
    You could have created some basic blocks of road (straight, curved... whatever) and always when you will closer to end of road, last road block create new random block in front of him. All road blocks just need to have defined start and end of road for corect rotating road for conecting path and for spawn position of next road.
    I create this way random generated maze :) its really simple but it work only for simple dezign.
     
  3. polan31

    polan31

    Joined:
    Nov 20, 2017
    Posts:
    149
    Something like this tutorial ?
     
    sameer9xm likes this.
  4. SVKsuli

    SVKsuli

    Joined:
    Mar 23, 2014
    Posts:
    63
    yea exactly, its depend of your dezign if you can use simple blocks like this, or you want to make all random angle curves and random shaped roads, then you need to look at mesh generating, i found some good tutorial for similar thing https://catlikecoding.com/unity/tutorials/swirly-pipe/ , you could do it same but not with pipe but with plane. I believe that there are exactly same tutorials like this for road creating.