LinearTeleport.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //***********************************************************
  2. // Filename: LinearTeleport.cs
  3. // Author: Moritz Kolvenbach, Marco Fendrich
  4. // Last changes: Wednesday, 8th of August 2018
  5. // Content: Class that calculates the line to be rendered and used to aim when preparing to teleport
  6. //***********************************************************
  7. using UnityEngine;
  8. /// <summary>
  9. /// Child class of renderableTeleport doing the actual calculations to aim the teleportation as a linecast
  10. /// </summary>
  11. public class LinearTeleport : RenderableTeleport
  12. {
  13. /// <summary>
  14. /// Sample a bunch of points along a line until you hit ground. At that point, cut off the linecast
  15. /// </summary>
  16. protected override void UpdateProjectionPoints()
  17. {
  18. // turn velocity to work into the direction of the controller
  19. normalisedVelocity = transform.TransformDirection(Vector3.forward);
  20. // delete points from last calculation
  21. projectionPoints.Clear();
  22. // add start point - the controller
  23. projectionPoints.Add(transform.position);
  24. // set point from which second point will be calculated to controller position
  25. Vector3 last = transform.position;
  26. float t = 0; // time for line calculation hasn't started yet
  27. // iterate through the maximum number of points being calculated
  28. for (int i = 0; i < pointCount; i++)
  29. {
  30. // increase t so that the next calculated point will have the distance set in editor
  31. t += pointSpacing / normalisedVelocity.magnitude;
  32. // calculate next point of line
  33. Vector3 next = CalculateNewPosition(transform.position, normalisedVelocity, t);
  34. Vector3 castHit; // hit point of line if colliding at or before next calculated point
  35. Vector3 norm; // normal of hit point
  36. bool endOnNavmesh; // indicator of whether last point calculated ended on navmesh
  37. // check whether something is being hit
  38. if (navMesh.Linecast(last, next, out endOnNavmesh, out castHit, out norm))
  39. {
  40. // if something is being hit, set last point in list to hit point
  41. projectionPoints.Add(castHit);
  42. // normal of hit point to be used when visualizing the currently selected target
  43. normalisedHitPoint = norm;
  44. // check whether point is on nav mesh and therefore teleportable
  45. pointOnNavMesh = endOnNavmesh;
  46. return;
  47. }
  48. else
  49. {
  50. // no collision, calculate next point
  51. projectionPoints.Add(next);
  52. }
  53. // set currently calculated point as base for the calculation of the new point
  54. last = next;
  55. }
  56. // if nothing is being hit and maximum number of points is reached, show unreachable target in midair with normal going upwards
  57. normalisedHitPoint = Vector3.up;
  58. pointOnNavMesh = false;
  59. }
  60. /// <summary>
  61. /// Linear motion equation applied to three dimensions
  62. /// </summary>
  63. /// <param name="p">Position from which the movement of the current update was started</param>
  64. /// <param name="v">Velocity with which the line is being calculated</param>
  65. /// <param name="t">Abstract time to be used to update point</param>
  66. /// <returns>Next point of line</returns>
  67. private static Vector3 CalculateNewPosition(Vector3 p, Vector3 v, float t)
  68. {
  69. Vector3 ret = new Vector3();
  70. for (int i = 0; i < 3; i++)
  71. ret[i] = p[i] + v[i] * t;
  72. return ret;
  73. }
  74. }