Advertisement
ChrisClark13

Untitled

Aug 15th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. public Vector3 CalculateMovement(Transform moverTransform, float currentDistanceAlongRail, Vector3 delta, out float newDistanceAlongRail, out Vector3 newDelta, out Vector3 directionNormal)
  2. {
  3. //Save this away for later since we don't want to do any actual math on the y axis.
  4. var deltaY = delta.y;
  5. delta.y = 0;
  6.  
  7. var dist = delta.magnitude;
  8. var currentNormal = GetDirectionNormalAtDistance(currentDistanceAlongRail);
  9. // print($"{currentNormal.x},{currentNormal.y},{currentNormal.z}");
  10.  
  11. //Are they going forwards, backwards, or just staying still?
  12. var dot = Vector3.Dot(delta, currentNormal);
  13.  
  14. //If they're staying still, then we can just exit early
  15. if (dot == 0f)
  16. {
  17. //new delta is just the y component because a dot product of 0
  18. //can mean that they're trying to move perpendicular to the rail so just have no XZ movement happen
  19. newDelta = new Vector3(0, deltaY, 0);
  20. newDistanceAlongRail = currentDistanceAlongRail;
  21. //Choose a normal along the path based on the way they're currently facing.
  22. directionNormal = (Vector3.Dot(moverTransform.forward, currentNormal) >= 0) ? currentNormal : -currentNormal;
  23. //Just return their current point along the rail
  24. return GetPointAtDistance(currentDistanceAlongRail).With(y: moverTransform.position.y + deltaY);
  25. }
  26. else if (dot < 0)
  27. {
  28. //We're going backwards along the rail!
  29. dist = -dist;
  30. }
  31. //Else: We're going fowards and we don't need to do any extra maths.
  32.  
  33. // print($"{dot}:{delta}*{currentNormal}, {currentDistanceAlongRail}");
  34.  
  35. newDistanceAlongRail = currentDistanceAlongRail + dist;
  36. if (!Looped)
  37. {
  38. newDistanceAlongRail = Mathf.Clamp(newDistanceAlongRail, 0f, Length);
  39. }
  40. else
  41. {
  42. newDistanceAlongRail = (newDistanceAlongRail % Length) + (newDistanceAlongRail < 0 ? Length : 0);
  43. }
  44.  
  45. var currentPoint = GetPointAtDistance(currentDistanceAlongRail);
  46. var newPoint = GetPointAtDistance(newDistanceAlongRail);
  47.  
  48. //Put the y component of the delta back in.
  49. newDelta = currentPoint.VectorTo(newPoint).With(y: deltaY);
  50. directionNormal = GetDirectionNormalAtDistance(newDistanceAlongRail) * Mathf.Sign(dist);
  51.  
  52. //Calculate their y change for them and return.
  53. return newPoint.With(y: moverTransform.position.y + deltaY);
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement