Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Sep 20, 2018 at 11:25 PM by polan31 for the following reason:

Problem is not reproducible or outdated

avatar image
0
Question by polan31 · Sep 15, 2018 at 05:46 PM · 2dscript.follow path

How to create a list of path follow points?

Hello all.

Noob here :)

I found a great script that allows me to create a PathFollow in 2D.

However, I would like every point that I add to appear as a childobject.

Could someone help me? I always have a problem with the list :(

Path Script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [System.Serializable]
 public class Path {
 
     [SerializeField, HideInInspector]
     List<Vector2> points;
 
     public Path(Vector2 centre)
     {
         points = new List<Vector2>
         {
             centre+Vector2.left,
             centre+(Vector2.left+Vector2.up)*.5f,
             centre + (Vector2.right+Vector2.down)*.5f,
             centre + Vector2.right
         };
     }
 
     public Vector2 this[int i]
     {
         get
         {
             return points[i];
         }
     }
 
     public int NumPoints
     {
         get
         {
             return points.Count;
         }
     }
 
     public int NumSegments
     {
         get
         {
             return (points.Count - 4) / 3 + 1;
         }
     }
 
     public void AddSegment(Vector2 anchorPos)
     {
         points.Add(points[points.Count - 1] * 2 - points[points.Count - 2]);
         points.Add((points[points.Count - 1] + anchorPos) * .5f);
         points.Add(anchorPos);
     }
 
     public Vector2[] GetPointsInSegment(int i)
     {
         return new Vector2[] { points[i * 3], points[i * 3 + 1], points[i * 3 + 2], points[i * 3 + 3] };
     }
 
     public void MovePoint(int i, Vector2 pos)
     {
         Vector2 deltaMove = pos - points[i];
         points[i] = pos;
 
         if (i % 3 == 0)
         {
             if (i + 1 < points.Count)
             {
                 points[i + 1] += deltaMove;
             }
             if (i - 1 >= 0)
             {
                 points[i - 1] += deltaMove;
             }
         }
         else
         {
             bool nextPointIsAnchor = (i + 1) % 3 == 0;
             int correspondingControlIndex = (nextPointIsAnchor) ? i + 2 : i - 2;
             int anchorIndex = (nextPointIsAnchor) ? i + 1 : i - 1;
 
             if (correspondingControlIndex >= 0 && correspondingControlIndex < points.Count)
             {
                 float dst = (points[anchorIndex] - points[correspondingControlIndex]).magnitude;
                 Vector2 dir = (points[anchorIndex] - pos).normalized;
                 points[correspondingControlIndex] = points[anchorIndex] + dir * dst;
             }
         }
     }
 
 }


PathCreator script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PathCreator : MonoBehaviour {
 
     [HideInInspector]
     public Path path;
 
     public void CreatePath()
     {
         path = new Path(transform.position);
     }
 }

PathEditor Script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEditor;
 
 [CustomEditor(typeof(PathCreator))]
 public class PathEditor : Editor {
 
     PathCreator creator;
     Path path;
 
     void OnSceneGUI()
     {
         Input();
         Draw();
     }
 
     void Input()
     {
         Event guiEvent = Event.current;
         Vector2 mousePos = HandleUtility.GUIPointToWorldRay(guiEvent.mousePosition).origin;
 
         if (guiEvent.type == EventType.MouseDown && guiEvent.button == 0 && guiEvent.shift)
         {
             Undo.RecordObject(creator, "Add segment");
             path.AddSegment(mousePos);
         }
     }
 
     void Draw()
     {
 
         for (int i = 0; i < path.NumSegments; i++)
         {
             Vector2[] points = path.GetPointsInSegment(i);
             Handles.color = Color.black;
             Handles.DrawLine(points[1], points[0]);
             Handles.DrawLine(points[2], points[3]);
             Handles.DrawBezier(points[0], points[3], points[1], points[2], Color.green, null, 2);
         }
 
         Handles.color = Color.red;
         for (int i = 0; i < path.NumPoints; i++)
         {
             Vector2 newPos = Handles.FreeMoveHandle(path[i], Quaternion.identity, .1f, Vector2.zero, Handles.CylinderHandleCap);
             if (path[i] != newPos)
             {
                 Undo.RecordObject(creator, "Move point");
                 path.MovePoint(i, newPos);
             }
         }
     }
 
     void OnEnable()
     {
         creator = (PathCreator)target;
         if (creator.path == null)
         {
             creator.CreatePath();
         }
         path = creator.path;
     }
 }





Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tormentoarmagedoom · Sep 15, 2018 at 05:53 PM 0
Share

Good day.

So many code... i will not read it all...

You say want to make them child ... child of what ? lets imagine you make it child of an object called pathPositions.

Once you have your List of Vector2 positions, called points, you only need to acces them all and convert it a child of pathPositions

 foreach (Vector2 pos in poitns)
 {
 pos.transform.parent = pathPositions.transform;
 }

Bye!

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

188 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is there a way I can add drawing mechanic like in "line rider" for a 3D game? 0 Answers

Spawn Random Enemies 2D 1 Answer

Make enemy damage the player,Enemy do damage to player Script 1 Answer

Multiple gameobjects but only one is screen wrapping. 0 Answers

How to activate an object after another is deactivated 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges