Question by
ilEtik · May 22, 2019 at 12:18 PM ·
c#positionvector3editor-scriptingprogramming
Moving an array of Vector3 with the GameObject (Path-System)
Hi, I'm currrently writing a Path-System for my NPC to move. I store the waypoints as an Array of Vector3. I'm moving the waypoints in an Editor script with the Handles tools. The Problem is that when I'm moving the Object where the Path is added, the waypoints don't move with the Obejct. I trired the InverseTransformVector and InverseTransformPosition but this doesn't worked well.
Path Script:
public class Path : MonoBehaviour
{
[SerializeField]
private Vector3[] wayPoints;
public Vector3[] WayPoints
{
get { return wayPoints; }
set
{
wayPoints = value;
/* for (int i = 0; i < WayPoints.Length; i++)
{
WayPoints[i] = transform.InverseTransformVector(transform.position);
}*/
}
}
[SerializeField]
private PathType pathType;
public PathType PathType { get { return pathType; } }
[SerializeField]
private WayPointsMoveType moveType;
public WayPointsMoveType MoveType { get { return moveType; } }
private void OnValidate()
{
WayPoints = wayPoints;
}
}
Path Editor Script:
[CustomEditor(typeof(Path))]
public class PathEditor : Editor
{
Path path;
Vector3[] WayPoints;
float polyLineWidth = 5f;
float size;
PathType pathType;
WayPointsMoveType moveType;
private void OnEnable()
{
path = (Path)target;
}
private void OnSceneGUI()
{
WayPoints = path.WayPoints;
pathType = path.PathType;
moveType = path.MoveType;
//Check if the Path has Waypoints.
if (WayPoints == null)
return;
ConnectWaypointsWithPolyLine();
DrawTransformHandles();
ConnectMainTransformWithWaypoints();
}
void ConnectWaypointsWithPolyLine()
{
//Sets the Color of the Polyline.
Handles.color = Color.cyan;
//Connects the all Waypoints with a polyline.
Handles.DrawAAPolyLine(polyLineWidth, WayPoints);
//Connects the last and the first Waypoint.
if (pathType == PathType.Loop)
Handles.DrawAAPolyLine(polyLineWidth, WayPoints[WayPoints.Length - 1], WayPoints[0]);
}
void DrawTransformHandles()
{
//Sets for every Waypoint a PositionHandle
for (int i = 0; i < WayPoints.Length; i++)
{
Quaternion rotation = Quaternion.Euler(Vector3.zero);
size = HandleUtility.GetHandleSize(WayPoints[i]) * .5f;
Vector3 snap = Vector3.one * 0.25f;
Vector3 targetPos = WayPoints[i];
switch (moveType)
{
case WayPointsMoveType.RectanglHandle:
//Handles.RectangleHandleCap(0, WayPoints[i] + new Vector3(1, 0, 0), rotation * Quaternion.LookRotation(Vector3.right), .5f, EventType.Repaint);
targetPos = Handles.FreeMoveHandle(WayPoints[i], rotation, size, snap, Handles.CircleHandleCap);
break;
case WayPointsMoveType.MoveTool:
targetPos = Handles.PositionHandle(WayPoints[i], rotation);
break;
}
WayPoints[i] = targetPos;
}
}
/// <summary>
/// Connects every Waypoint with the Transform from the main Object
/// </summary>
void ConnectMainTransformWithWaypoints()
{
Handles.color = Color.red;
for (int i = 0; i < WayPoints.Length; i++)
{
Handles.DrawAAPolyLine(WayPoints[i], path.gameObject.transform.position);
Handles.DrawWireDisc(WayPoints[i], new Vector3(0, 1, 0), 1);
}
}
[DrawGizmo(GizmoType.InSelectionHierarchy | GizmoType.Active)]
static void OnDrawGizmos(Path target, GizmoType gizmoType)
{
Vector3[] _waypoints = target.WayPoints;
//Draws a Flag Icon for every Waypoint.
foreach (Vector3 vector in _waypoints)
Gizmos.DrawIcon(vector, FilePaths.EditorRessourcesPath + "/FlagIcon.png");
}
}
Here is what the Path looks like in the Scene View:
screenshot-5.jpg
(419.0 kB)
Comment
Your answer
Follow this Question
Related Questions
transform.Translate changing object position by incorrect amount 1 Answer
gameobject position lerp 0 Answers
Saving old Position 1 Answer
Quick question with a Camera Following script 1 Answer
Get Vector3s in range 1 Answer