Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public Vector3 CalculateMovement(Transform moverTransform, float currentDistanceAlongRail, Vector3 delta, out float newDistanceAlongRail, out Vector3 newDelta, out Vector3 directionNormal)
- {
- //Save this away for later since we don't want to do any actual math on the y axis.
- var deltaY = delta.y;
- delta.y = 0;
- var dist = delta.magnitude;
- var currentNormal = GetDirectionNormalAtDistance(currentDistanceAlongRail);
- // print($"{currentNormal.x},{currentNormal.y},{currentNormal.z}");
- //Are they going forwards, backwards, or just staying still?
- var dot = Vector3.Dot(delta, currentNormal);
- //If they're staying still, then we can just exit early
- if (dot == 0f)
- {
- //new delta is just the y component because a dot product of 0
- //can mean that they're trying to move perpendicular to the rail so just have no XZ movement happen
- newDelta = new Vector3(0, deltaY, 0);
- newDistanceAlongRail = currentDistanceAlongRail;
- //Choose a normal along the path based on the way they're currently facing.
- directionNormal = (Vector3.Dot(moverTransform.forward, currentNormal) >= 0) ? currentNormal : -currentNormal;
- //Just return their current point along the rail
- return GetPointAtDistance(currentDistanceAlongRail).With(y: moverTransform.position.y + deltaY);
- }
- else if (dot < 0)
- {
- //We're going backwards along the rail!
- dist = -dist;
- }
- //Else: We're going fowards and we don't need to do any extra maths.
- // print($"{dot}:{delta}*{currentNormal}, {currentDistanceAlongRail}");
- newDistanceAlongRail = currentDistanceAlongRail + dist;
- if (!Looped)
- {
- newDistanceAlongRail = Mathf.Clamp(newDistanceAlongRail, 0f, Length);
- }
- else
- {
- newDistanceAlongRail = (newDistanceAlongRail % Length) + (newDistanceAlongRail < 0 ? Length : 0);
- }
- var currentPoint = GetPointAtDistance(currentDistanceAlongRail);
- var newPoint = GetPointAtDistance(newDistanceAlongRail);
- //Put the y component of the delta back in.
- newDelta = currentPoint.VectorTo(newPoint).With(y: deltaY);
- directionNormal = GetDirectionNormalAtDistance(newDistanceAlongRail) * Mathf.Sign(dist);
- //Calculate their y change for them and return.
- return newPoint.With(y: moverTransform.position.y + deltaY);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement