Last active
July 9, 2022 03:37
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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