Hello i am trying to do this in Pic1
but i have been getting the error argument is
out of range in my code(which is below
Pic1) i do not know how to fix it,
does anyone know how to fix this??
-Pic1
[77537-capture49.png*_|77537]
Here is my code so you know what i
have done.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Spline : MonoBehaviour
{
[System.Serializable]
public class SplinePoint
{
public Transform t;
public Transform cpAhead;
public Transform cpBehind;
public int seg;
public SplinePoint(Transform thetrans, Transform a, Transform b, int s)
{
t = thetrans;
cpAhead = a;
cpBehind = b;
seg = s;
}
}
public List<Transform> controlPointsList;
public List<SplinePoint> splinePoints;
public Transform segment;
public int segCount = 2;
public int curveSegments = 3;
public float updateRate = 60f;
public bool setup = false;
public bool running = false;
void Start()
{
splinePoints = new List<SplinePoint>();
CreateObjects(segment);
}
public void CreateObjects(Transform original)
{
for (int i = 0; i < controlPointsList.Count - 1; i++)
{
for (int j = 0; j < segCount; j++)
{
var a = Instantiate(original, Vector3.zero, Quaternion.identity) as Transform;
splinePoints.Add(new SplinePoint(a.transform, controlPointsList[i + 1], controlPointsList*, j));*
}
}
setup = true;
}
void Update()
{
if (setup && !running)
{
UpdateSplinePos();
}
}
void UpdateSplinePos()
{
for (int i = 0; i < splinePoints.Count; i++)
{
SetPosition(splinePoints*);*
}
}
SplinePoint GetSplinePoint(int s)
{
SplinePoint sp = null;
if (splinePoints.Count > 0)
{
for (int i = 0; i < splinePoints.Count; i++)
{
if (splinePoints*.seg == s)*
{
sp = splinePoints*;*
break;
}
}
}
return sp;
}
void OnDrawGizmos()
{
Gizmos.color = Color.white;
for (int i = 0; i < controlPointsList.Count; i++)
{
Gizmos.DrawWireSphere(controlPointsList*.position, 0.3f);*
}
for (int i = 0; i < controlPointsList.Count - 1; i++)
{
Gizmos.DrawLine(controlPointsList*.position, controlPointsList[i + 1].position);*
}
Gizmos.color = Color.red;
for (int i = 0; i < controlPointsList.Count - 1; i++)
{
List associatedNodes = NodesAssociated(controlPointsList*);*
List associatedNodesAhead = NodesAssociated(controlPointsList[i + 1]);
Vector3 p0 = associatedNodes[(i - 1)].t.position;// point left
Vector3 p1 = controlPointsList*.position;// Control point*
Vector3 p2 = associatedNodesAhead[(i + 1)].t.position; // point right
Vector3 lastPos = Vector3.zero;
if (associatedNodes.Count > 0 && associatedNodesAhead.Count > 0)
{
Gizmos.DrawLine(associatedNodes[associatedNodes.Count - 1].t.position, associatedNodesAhead[0].t.position);
//Gizmos.DrawWireSphere(associatedNodes[associatedNodes.Count - 1].t.position, 1f);
//Gizmos.DrawWireSphere(associatedNodesAhead[0].t.position, 1f);
}
for (int n = 0; n < curveSegments; n++)
{
float t = (float)n / curveSegments;
Vector3 newPos = GetPoint(p0, p1, p2, t);
if (t == 0)
{
lastPos = newPos;
continue;
}
Gizmos.DrawLine(lastPos, newPos);
lastPos = newPos;
}
}
}
void SetPosition(SplinePoint sp)
{
float dist = Vector3.Distance(sp.cpAhead.position, sp.cpBehind.position);
float step = dist / (segCount + 1);
Vector3 dir = (sp.cpAhead.position - sp.cpBehind.position).normalized;
sp.t.position = sp.cpBehind.position + ((dir * step) * (sp.seg + 1));
sp.t.rotation = Quaternion.LookRotation(dir);
}
List NodesAssociated(Transform behindNode)
{
List p = new List();
for (int i = 0; i < splinePoints.Count; i++)
{
if (splinePoints*.cpBehind == behindNode)*
{
p.Add(splinePoints*);*
}
}
return p;
}
public static Vector3 GetPoint(Vector3 p0, Vector3 p1, Vector3 p2, float t)
{
t = Mathf.Clamp01(t);
float oneMinusT = 1f - t;
return
oneMinusT * oneMinusT * p0 +
2f * oneMinusT * t * p1 +
t * t * p2;
}
}
@b1gry4n
_*