- Home /

# How do i make a Linear Spline??

Hello i have been trying to modify this script i have so it is a Linear spline instead of a (Catmull)Bezier like in Pic1 but my results are in Pic2, would anyone know how to do/fix this?

(My Error is obviously in line 29 to 39 the calculation of the spline, i know this question is rather stupid since it uses simple math)

-Pic1 **(what i am trying to achieve)** -Pic2 **(the results i am getting)**

Here is my code so you know what i have done. i believe my mistake is in the **Vector3 ReturnCatmullRom**.

```
using UnityEngine;
using System.Collections.Generic;
public class LinearSpline : MonoBehaviour {
public List<Transform> controlPointsList = new List<Transform>();
public int segCount = 2;
public bool isLooping = true;
void OnDrawGizmos()
{
Gizmos.color = Color.white;
for (int i = 0; i < controlPointsList.Count; i++)
{
Gizmos.DrawWireSphere(controlPointsList[i].position, 0.3f);
}
for (int i = 0; i < controlPointsList.Count; i++)
{
if ((i == 0 || i == controlPointsList.Count - 2 || i == controlPointsList.Count - 1) && !isLooping)
{
continue;
}
DisplayLinearSpline(i);
}
}
Vector3 ReturnLinearRom(float t, Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3)
{
Vector3 a = p0;
Vector3 b = p1;
Vector3 c = p2;
Vector3 d = p3;
Vector3 pos = a + b + c + d;
return pos;
}
void DisplayLinearSpline(int pos)
{
Vector3 p0 = controlPointsList[ClampListPos(pos - 1)].position;
Vector3 p1 = controlPointsList[pos].position;
Vector3 p2 = controlPointsList[ClampListPos(pos + 1)].position;
Vector3 p3 = controlPointsList[ClampListPos(pos + 2)].position;
Vector3 lastPos = Vector3.zero;
for (int t = 0; t < segCount; t++)
{
float n = (float)t / segCount;
Vector3 newPos = ReturnLinearRom(n, p0, p1, p2, p3);
if (n == 0)
{
lastPos = newPos;
continue;
}
Gizmos.color = Color.white;
Gizmos.DrawLine(lastPos, newPos);
lastPos = newPos;
Gizmos.DrawSphere(newPos, 0.3f);
}
Gizmos.DrawLine(lastPos, p2);
}
int ClampListPos(int pos)
{
if (pos < 0)
{
pos = controlPointsList.Count - 1;
}
if (pos > controlPointsList.Count)
{
pos = 1;
}
else if (pos > controlPointsList.Count - 1)
{
pos = 0;
}
return pos;
}
}
```

**Answer** by ldeboer
·
Aug 31, 2016 at 08:59 AM

You realize the original code was drawing a spline out to screen in a loop because it needed to draw little line segments to make the curve so it had to loop. You have no need to do that you are drawing a line between the control points so use Gizmos.DrawLine directly

// Draw line between each control point for (int i = 0; i < controlPointsList.Count-1; i++) { Gizmos.DrawLine(controlPointsList[i].position, controlPointsList[i+1].position); } // Finally line between first and last control point Gizmos.DrawLine(controlPointsList[controlPointsList.Count-1].position, controlPointsList[0].position);You sort of show the midpoint as well and if you want that then simply add the two control positions together and divide by two.

I should add that as a catmull-rom MUST PASS THRU any middle points you could also just use your catmull-rom code as it was but set the two middle control points of the spline to the same value of the mid point between the two points or even the point 1/4 and 3/4 of the way along line if you wanted nice even little line segments.

Ah forgot about that part. anyway Thanks for the help.

But is there a way to do it in a calculation? ins$$anonymous$$d of just drawing the lines to each point.

There is no such thing as a linear spline ... some spline types can be made to draw a line by organizing the control points in a special way. So the is NO CALCULATION called linear spline in the way you are trying to use it as a parametric equation. That is what the loop function inside the catmull-rom code did it blends the control points.

So your options are

Use a spline like catmull-rom and set the control points from the two end control points

Draw a simple line between the points

Recursively divid the line into small sections

For 1 and 3 what you want is points at set distances along the line so lets look at that using you control points from code above

This is the calculation for the point midway between any two of your control points

Transform mid.position = (controlPointsList[i].position + controlPointList[i+1].position)/2;Now if you want the point half way between that point and each end, so that would be points 1/4 of the way along and 3/4 of the way along use the same calc but use the above midpoint

So the 1/4 point would be .. half way between first control and the mid point

Transform onequarter.position = (controlPointsList[i].position + mid.position)/2;The 3/4 point would be .. half way between second control point and mid point

Transform threequarter.position = (controlPointsList[i+1].position + mid.position)/2;

So you could modify your loop code to recurse the subdivision process which is called tweening. Generally it would be easier to organize the function in paramateric form going from 0 to 1 to do that so

xt = (x2-x1)*t + x1 where 0 <= t <= 1

In your code it would look like this

double t; Transform tpos.position; tpos.x = (controlPointsList[i+1].position.x -controlPointList[i].position.x)*t + controlPointList[i].position.x; tpos.y = (controlPointsList[i+1].position.y -controlPointList[i].position.y)*t + controlPointList[i].position.y; tpos.z = (controlPointsList[i+1].position.z -controlPointList[i].position.z)*t + controlPointList[i].position.z;

If you make t = 0.1 .. tpos will be the point 1/10 of the way along the line, t= 0.25 will be the point quarter of the way along the line etc. So you can loop it like your spline code just changing the t value.

OR you use standard catmull-rom spline you already have using these four points

controlPointsList[i].position

onequarter.position

threequarter.position

controlPointsList[i+1].position

The catmull-rom spline is **GUARANTEED** to be a straight line and the draw segments will be evenly spaced.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

How do i do this type of spline?? 1 Answer

Adding segments in missing spots 1 Answer

How do i create points along a spline??? 0 Answers

Problem with curved Cornered Spline that i need help with 0 Answers

How do i fix Error CS0029? 1 Answer