Advertisement
Staggart

Create Spline from code

Apr 19th, 2024
514
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.73 KB | None | 0 0
  1. GameObject gameObject = new GameObject("River");
  2.  
  3. SplineContainer splineContainer = gameObject.AddComponent<SplineContainer>();
  4. splineContainer.Splines = null;
  5.  
  6. //Create a basic spline with a wiggly shape
  7. int knots = 5;
  8. float amplitude = 2f;
  9. float length = 50f;
  10. Spline spline = new Spline(knots, false);
  11.  
  12. for (int i = 0; i <= knots; i++)
  13. {
  14.     float t = (float)i / (float)knots;
  15.    
  16.     BezierKnot knot = new BezierKnot();
  17.    
  18.     knot.Position = new Vector3(Mathf.Sin(t * knots * 2f) * amplitude, 0f, (t * length) - (length * 0.5f));
  19.    
  20.     spline.Add(knot, TangentMode.Linear);
  21. }
  22.  
  23. //Automatically recalculate tangents
  24. spline.SetTangentMode(new SplineRange(0, spline.Count), TangentMode.AutoSmooth);
  25.  
  26. splineContainer.AddSpline(spline);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement