DistortedTeleport.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //***********************************************************
  2. // Filename: DistortedTeleport.cs
  3. // Author: Moritz Kolvenbach, Marco Fendrich
  4. // Last changes: Thursday, 9th of August 2018
  5. // Content: Class that adds rotation and a tilt via controller to AdjustableSpeedTeleport but only after maximum of parabola
  6. //***********************************************************
  7. using UnityEngine;
  8. /// <summary>
  9. /// Child class of CurveTeleport adding rotation and a tilt via controller but only after maximum of parapola
  10. /// </summary>
  11. public class DistortedTeleport : CurveTeleport
  12. {
  13. /// <summary>
  14. /// Entire calculation is analogue to <see class = "ParabolicTeleport"></see> except:
  15. /// the commented lines
  16. /// acceleration was replaced with distortedAcceleration (simply being tilted according to controller)
  17. /// </summary>
  18. protected override void UpdateProjectionPoints()
  19. {
  20. normalisedVelocity = transform.TransformDirection(initialVelocity);
  21. projectionPoints.Clear();
  22. projectionPoints.Add(transform.position);
  23. // acceleration from maximum of parabola onwards; starting with normal acceleration
  24. Vector3 distortedAcceleration = acceleration;
  25. Vector3 last = transform.position;
  26. // trigger for points after maximum
  27. bool distortionTriggered = false;
  28. for (int i = 0; i < pointCount; i++)
  29. {
  30. float t = pointSpacing / normalisedVelocity.magnitude;
  31. normalisedVelocity = normalisedVelocity + distortedAcceleration * t;
  32. // if maximum wasn't reached before and velocity is now negative - therefore past maximum
  33. if (normalisedVelocity.y < 0 && !distortionTriggered)
  34. {
  35. // set acceleration to tilt with controller
  36. distortedAcceleration =
  37. Quaternion.Euler(limitRotation(transform.rotation.eulerAngles, 80)) * acceleration * 10F;
  38. // remove latest point as this one will be past the maximum yet calculated with normal acceleration
  39. projectionPoints.RemoveAt(projectionPoints.Count-1);
  40. distortionTriggered = true;
  41. }
  42. Vector3 next = last + normalisedVelocity * t + 0.5F * distortedAcceleration * t * t;
  43. Vector3 castHit;
  44. Vector3 norm;
  45. bool endOnNavmesh;
  46. if (navMesh.Linecast(last, next, out endOnNavmesh, out castHit, out norm))
  47. {
  48. // add rotation according to the angle in which the last point stands to the hit point on the ground times 1.5
  49. // get angle of last point to hit point
  50. Vector2 projectedEnd = new Vector2(castHit.x - projectionPoints[projectionPoints.Count - 1].x, castHit.z - projectionPoints[projectionPoints.Count - 1].z);
  51. // get angle of controller
  52. Vector2 controllerDirection = new Vector2(transform.forward.x, transform.forward.z);
  53. // get difference between those two angles to get actual offset independent of world space
  54. rotation = Vector2.SignedAngle(projectedEnd, controllerDirection) * 1.5F;
  55. projectionPoints.Add(castHit);
  56. normalisedHitPoint = norm;
  57. pointOnNavMesh = endOnNavmesh;
  58. return;
  59. }
  60. else
  61. {
  62. projectionPoints.Add(next);
  63. }
  64. last = next;
  65. }
  66. normalisedHitPoint = Vector3.up;
  67. pointOnNavMesh = false;
  68. }
  69. }