Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

How to make Path in unity

Discussion in 'Scripting' started by softzonebeta, Apr 20, 2018.

  1. softzonebeta

    softzonebeta

    Joined:
    Dec 27, 2017
    Posts:
    6
    Hi , I want to make path visualy in editor so any object or character can use that in the game to move.
    any help how to get start making path. Visually in unity. I dont want to use any package form asset store
     
  2. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
  3. softzonebeta

    softzonebeta

    Joined:
    Dec 27, 2017
    Posts:
    6
    Navmesh no.

    I want a path to be make and edit in editor ,
    if object collide with 1st point on path then it should follow that path to complete its movement
     
  4. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    so you want a path check point system?
     
  5. softzonebeta

    softzonebeta

    Joined:
    Dec 27, 2017
    Posts:
    6
    i am talking about this type of path
     
  6. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    So, if you can stand to watch that bozo type his dialog, it looks like that will give you a start making paths in around 50 lines of code.

    (I can't be sure, as I couldn't stand to watch it for more than a few seconds.)
     
  7. chelnok

    chelnok

    Joined:
    Jul 2, 2012
    Posts:
    680
    Search "waypoint system". You'll find tutorials from youtube, free and paid assets from Asset store (even that you don't want to use any packet from store, you can learn how others have done it). For visuals in editor, search for gizmos and debug.drawray or drawline.
     
  8. softzonebeta

    softzonebeta

    Joined:
    Dec 27, 2017
    Posts:
    6
    hahahah Ok

    i will try waypoint system
     
  9. softzonebeta

    softzonebeta

    Joined:
    Dec 27, 2017
    Posts:
    6
    Guys I can Create Waypoints now but i have few problems to solve i am posting both C# Script
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PathMovement_XY : MonoBehaviour
    6. {
    7.  
    8.     // Use this for initialization
    9.     public PathEditor_XY WaypointPath;
    10.     public int CurrentWaypointID = 0;
    11.     public float speed;
    12.     [SerializeField]
    13.     private float reachDistance = 1.0f;
    14.     public float rotationSpeed = 5.0f;
    15.     public string PathName;
    16.     Vector2 Lastposition;
    17.     Vector2 CurrentPosition;
    18.     void Start()
    19.     {
    20.         // WaypointPath = GameObject.Find(PathName).GetComponent<PathEditor>();
    21.         Lastposition = transform.position;
    22.  
    23.     }
    24.  
    25.     // Update is called once per frame
    26.     void Update()
    27.     {
    28.         float distance = Vector2.Distance(WaypointPath.Waypoints[CurrentWaypointID].position, transform.position);
    29.         transform.position = Vector2.MoveTowards(transform.position, WaypointPath.Waypoints[CurrentWaypointID].position, Time.deltaTime * speed);
    30.         if (distance <= reachDistance)
    31.         {
    32.             CurrentWaypointID++;
    33.         }
    34.         if (CurrentWaypointID >= WaypointPath.Waypoints.Count)
    35.         {
    36.             CurrentWaypointID = 0;
    37.         }
    38.     }
    39.  
    40.    
    41. }
    42.  
    and
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PathEditor_XY : MonoBehaviour {
    6.  
    7.     // Use this for initialization
    8.     public float GizmoRadius = 0.4f;   // Gizmo Radius
    9.     public Color ray = Color.green;    // Ray Color
    10.     public List<Transform> Waypoints = new List<Transform>();   // Creating Waypoint list
    11.     Transform[] theArray;          // Array for Transform
    12.     void OnDrawGizmos()     // Drawing Gizmo on Screen In editor.
    13.     {
    14.         Gizmos.color = ray;
    15.         theArray = GetComponentsInChildren<Transform>();   // Getting Components in childern.
    16.         Waypoints.Clear();                                 // Clear Waypoints At Start.
    17.         foreach (Transform wayp in theArray)
    18.         {
    19.             if (wayp != this.transform)
    20.             {
    21.                 Waypoints.Add(wayp);
    22.             }
    23.         }
    24.         for (int i = 0; i < Waypoints.Count; i++)  // creating for loop
    25.         {
    26.             Vector2 position = Waypoints[i].position;
    27.             if (i > 0)
    28.             {
    29.                 Vector2 previous = Waypoints[i - 1].position;
    30.                 Gizmos.DrawLine(previous, position);
    31.                 Gizmos.DrawSphere(position, GizmoRadius);
    32.             }
    33.         }
    34.     }
    35. }
    36.  

    The Problems are

    I can not directly select waypoints in editor i have to select them in hierachy
    2nd I want to follow waypoints only if i collide with 1st point of waypoint
    How i can also make ping pong type movement and flip sprites if reaches the end waypoint