Skip to content

Instantly share code, notes, and snippets.

@Tattomoosa
Last active July 9, 2022 03:37
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Tattomoosa/9fef0b0811adb562e4ac9a35fcf2e4b0 to your computer and use it in GitHub Desktop.
Save Tattomoosa/9fef0b0811adb562e4ac9a35fcf2e4b0 to your computer and use it in GitHub Desktop.
A Unity class that calculates a forward vector from the tangent of a point on a 'BGCurve' closest to a given point, used in my 2.5D Platformer Engine.
using UnityEngine;
using BansheeGz.BGSpline.Curve;
[RequireComponent(typeof(BansheeGz.BGSpline.Curve.BGCurve))]
public class Platformer2Point5D_Path : MonoBehaviour {
private BGCurve curve;
private BGCurveBaseMath math;
void Start () {
curve = GetComponent<BGCurve>();
math = new BGCurveBaseMath(curve, new BGCurveBaseMath.Config(BGCurveBaseMath.Fields.PositionAndTangent));
}
// This is the important method
public Vector3 CalculateForwardFromClosestPoint(Vector3 outsidePoint, out Vector3 curvePoint)
{
Vector3 forward = CalculateTangentFromClosestPoint(outsidePoint, out curvePoint);
forward.y = 0;
return forward.normalized;
}
private Vector3 CalculateTangentFromClosestPoint(Vector3 outsidePoint, out Vector3 curvePoint)
{
float curvePointDistance;
curvePoint = math.CalcPositionByClosestPoint(outsidePoint, out curvePointDistance);
Vector3 tangent = math.CalcTangentByDistance(curvePointDistance);
return tangent;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment