//get math
var math = GetComponent<BGCcMath>();
//====Total length
//get total spline's length
var totalDistance = math.GetDistance();
//====Position (World)
//get position at 1 meter from the start
var positionAtOneMeter = math.CalcByDistance(BGCurveBaseMath.Field.Position, 1);
//get position at the center of the spline
var positionAtSplineCenter = math.CalcByDistanceRatio(BGCurveBaseMath.Field.Position, .5f);
//====Tangent (World)
//get tangent at 1 meter from the start
var tangentAtOneMeter = math.CalcByDistance(BGCurveBaseMath.Field.Tangent, 1);
//get tangent at the center of the spline
var tangentAtSplineCenter = math.CalcByDistanceRatio(BGCurveBaseMath.Field.Tangent, .5f);
//====Position and Tangent (World)
//get position and tangent at 1 meter from the start
Vector3 tangAtOneMeter;
var posAtOneMeter = math.CalcPositionAndTangentByDistance(1, out tangAtOneMeter);
//get position and tangent at the center of the spline
Vector3 tangAtSplineCenter;
var posAtSplineCenter = math.CalcPositionAndTangentByDistanceRatio(.5f, out tangAtSplineCenter);
//====Section index
//get section index at 1 meter from the start
var sectionIndexAtOneMeterFromStart = math.CalcSectionIndexByDistance(1);
//get section index at the center of the spline
var sectionIndexAtSplineCenter = math.CalcSectionIndexByDistanceRatio(.5f);
//====Distance to point
//get distance at point #1 (#0-start point, #1-second point from start)
//math.Math[0] return info about 1st section
var distanceToFirstPoint = math.Math[0].DistanceFromEndToOrigin;
//====Position and Distance to closest point
//use this picture as a reference http://www.bansheegz.com/images/DGMathCalcPositionByClosestPoint.jpg
//assuming green point is Vector3.zero
//positionYellow - the position of the yellow point
var positionYellow = math.CalcPositionByClosestPoint(Vector3.zero);
//distanceYellow - the length of yellow arrow
float distanceYellow;
//positionYellow2 - the position of the yellow point
var positionYellow2 = math.CalcPositionByClosestPoint(Vector3.zero, out distanceYellow);
//distanceGreen- the length of green arrow
var distanceGreen = Vector3.Distance(math.CalcPositionByClosestPoint(Vector3.zero), Vector3.zero);
using UnityEngine;
using BansheeGz.BGSpline.Components;
[RequireComponent(typeof(BGCcMath))]
public class Test : MonoBehaviour
{
public Transform ObjectToMove;
private float distance;
void Update()
{
//increase distance
distance += 5 * Time.deltaTime;
//calculate position and tangent
Vector3 tangent;
ObjectToMove.position = GetComponent<BGCcMath>().CalcPositionAndTangentByDistance(distance, out tangent);
//this is a version for 3D. For 2D, comment this line and uncomment the next one
ObjectToMove.rotation = Quaternion.LookRotation(tangent);
//ObjectToMove.rotation = Quaternion.AngleAxis(Mathf.Atan2(tangent.y, tangent.x) * Mathf.Rad2Deg, Vector3.forward);
}
}
private BGCurve curve;
void Start()
{
curve = GetComponent<BGCurve>();
//cache the point
var point = curve[0];
//position
print("Position World=" + point.PositionWorld);
print("Position Local=" + point.PositionLocal);
print("Position Local Transformed=" + point.PositionLocalTransformed);
//control type
print("Control Type=" + point.ControlType);
//control 1 (Inbound)
print("Control 1 World=" + point.ControlFirstWorld);
print("Control 1 Local=" + point.ControlFirstLocal);
print("Control 1 Local Transformed=" + point.ControlFirstLocalTransformed);
//control 2 (Outbound)
print("Control 2 World=" + point.ControlSecondWorld);
print("Control 2 Local=" + point.ControlSecondLocal);
print("Control 2 Local Transformed=" + point.ControlSecondLocalTransformed);
//all properties have corresponding 'setter'
//see 'Coordinates' section for detailed explanation of coordinate spaces
}
//cache the points and count
var points = curve.Points;
var pointsCount = points.Length;
//use 'for' loop
for (var i = 0; i < pointsCount; i++)
{
var point = points[i];
//do something here
}
//closed (last first points connected)
print("Closed:" + curve.Closed);
//use curve.Fields to access fields
print("# of curstom fields:" + curve.FieldsCount);
//2D mode
print("2D mode:" + curve.Mode2D);
//snapping (use curve.SnapXXX to access other snap related properties)
print("Snap type:" + curve.SnapType);
//points mode (Inlined, Components, GameObjects)
print("Points mode:" + curve.PointsMode);
private BGCurve curve;
private BGCcMath math;
void Start()
{
//add spline
curve = gameObject.AddComponent<BGCurve>();
//add points
curve.AddPoint(new BGCurvePoint(curve, Vector3.zero, BGCurvePoint.ControlTypeEnum.Absent));
curve.AddPoint(new BGCurvePoint(curve, Vector3.right, BGCurvePoint.ControlTypeEnum.BezierSymmetrical,
new Vector3(-0.5f, 0.5f, 0), new Vector3(0.5f, -0.5f, 0)));
//add math solver
math = gameObject.AddComponent<BGCcMath>();
//print calculated values
print("Spline's Length=" + math.GetDistance());
print("Position at the middle=" + math.CalcByDistanceRatio(BGCurveBaseMath.Field.Position, .5f));
print("Point, closest to Vector3.one, =" + math.CalcPositionByClosestPoint(Vector3.one));
}
var point = curve[0];
//all supported types listed below
//C#
var @bool = point.GetBool("yourFieldName");
var @int = point.GetInt("yourFieldName");
var @float = point.GetFloat("yourFieldName");
var @string = point.GetField<string>("yourFieldName");
//Unity structs
var bounds = point.GetBounds("yourFieldName");
var color = point.GetColor("yourFieldName");
var vector3 = point.GetVector3("yourFieldName");
var quaternion = point.GetQuaternion("yourFieldName");
//Unity objects
var animationCurve = point.GetField<AnimationCurve>("yourFieldName");
var component = point.GetField<Component>("yourFieldName");
var go = point.GetField<GameObject>("yourFieldName");
// BG Curve related
var bgCurve = point.GetField<BGCurve>("yourFieldName");
var pointComponent = point.GetField<BGCurvePointComponent>("yourFieldName");
var pointGo = point.GetField<BGCurvePointGO>("yourFieldName");
//use corresponding SetXXX for setters
using UnityEngine;
using BansheeGz.BGSpline.Curve;
using BansheeGz.BGSpline.Components;
/// <summary>
/// Spawn a curve+components and move an object along it
/// </summary>
public class CurveSpawner : MonoBehaviour
{
//object which needs to be moved
public Transform ObjectToMove;
//if this flag is set, single Trs component is used, otherwise 3 components are used (cursor+changelinear+translate)
public bool UseTrsComponent;
void Start()
{
//add a curve
var curve = gameObject.AddComponent<BGCurve>();
//add points
curve.AddPoint(new BGCurvePoint(curve, Vector3.zero));
curve.AddPoint(new BGCurvePoint(curve, new Vector3(10,10,10)));
//add math
var math = AddCurveComponent<BGCcMath>();
//add component(s), required to move an object
if (UseTrsComponent) AddTrs(math);
else AddCursorComponents(math);
}
//adds 1 single component (Trs), which requires to move an object along a curve
private void AddTrs(BGCcMath math)
{
//add Trs
var trs = AddCurveComponent<BGCcTrs>(math);
trs.ObjectToManipulate = ObjectToMove;
}
//adds 3 component, which requires to move an object along a curve
private void AddCursorComponents(BGCcMath math)
{
//add cursor
var cursor = AddCurveComponent<BGCcCursor>(math);
//add changelinear
var cursorChange = AddCurveComponent<BGCcCursorChangeLinear>(cursor);
//add translate
var translateObject = AddCurveComponent<BGCcCursorObjectTranslate>(cursor);
translateObject.ObjectToManipulate = ObjectToMove;
}
//adds curve's component and assign a parent
private T AddCurveComponent<T>(BGCc parent = null) where T : BGCc
{
var component = gameObject.AddComponent<T>();
if (parent != null) component.SetParent(parent);
return component;
}
}