I had a few transforms inside a struct that I was using for position data. When I went to test it though, it all worked fine but when I moved the transforms nothing happened as if it wasn’t updating. I tried using a class instead but that didn’t work either.
using System;
using System.Linq;
using System.Collections.Generic;
using UnityEngine;
//Or rather a Spline now
public class DrawCurve : MonoBehaviour
{
public List<Curve> curves = new List<Curve>();
public int resolution = 80;
public Material mat;
private List<Vector3> points = new List<Vector3>();
private void FixedUpdate()
{
for (int i = 0; i < curves.Count; i++)
{
Draw(curves[i]);
}
LineRenderer lr = gameObject.GetComponent<LineRenderer>();
lr.startWidth = 0.25f;
lr.endWidth = 0.25f;
lr.startColor = Color.white;
lr.endColor = Color.white;
lr.material = mat;
lr.positionCount = resolution;
lr.SetPositions(points.ToArray());
}
private void Draw(Curve curve)
{
float t = 0;
curve.resolution = resolution / curves.Count;
for (int j = 0; j < curve.resolution; j++)
{
//Polynomial (TODO: Implement the matrix multiplication instead because its faster, cleaner and cooler)
Vector3 point = curve.P0.position +
t * (-3 * curve.P0.position + 3 * curve.P1.position) +
Mathf.Pow(t, 2) * (3 * curve.P0.position - 6 * curve.P1.position + 3 * curve.P2.position) +
Mathf.Pow(t, 3) * (-curve.P0.position + 3 * curve.P1.position - 3 * curve.P2.position + curve.P3.position);
t += 1f / curve.resolution;
points.Add(point);
}
}
}
[Serializable]
public class Curve
{
public Transform P0;
public Transform P1;
public Transform P2;
public Transform P3;
public int resolution;
}
If anyone knows what I’m doing wrong, I’d appreciate the help a lot!
Thanks in advance!