CurveTeleport.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //***********************************************************
  2. // Filename: CurveTeleport.cs
  3. // Author: Moritz Kolvenbach, Marco Fendrich
  4. // Last changes: Wednesday, 8th of August 2018
  5. // Content: Class that adds a tilt and a rotation via controller to AdjustableSpeedTeleport
  6. //***********************************************************
  7. using UnityEngine;
  8. /// <summary>
  9. /// Child class of AdjustableSpeedTeleport adding a tilt and a rotation via controller
  10. /// </summary>
  11. public class CurveTeleport : AdjustableSpeedTeleport
  12. {
  13. private Vector3 normalisedAcceleration;
  14. /// <summary>
  15. /// Entire calculation is analogue to <see class = "ParabolicTeleport"></see> except:
  16. /// the commented lines
  17. /// acceleration was replaced with normalisedAcceleration (simply being tilted according to controller)
  18. /// </summary>
  19. protected override void UpdateProjectionPoints()
  20. {
  21. // tilt rotation corresponding to tilt of controller but limit this tilt to 80°
  22. normalisedAcceleration = Quaternion.Euler(limitRotation(transform.rotation.eulerAngles, 80)) * acceleration;
  23. normalisedVelocity = transform.TransformDirection(initialVelocity);
  24. projectionPoints.Clear();
  25. projectionPoints.Add(transform.position);
  26. Vector3 last = transform.position;
  27. float t = 0;
  28. for (int i = 0; i < pointCount; i++)
  29. {
  30. t += pointSpacing / CalculateNewVelocity(normalisedVelocity, normalisedAcceleration, t).magnitude;
  31. Vector3 next = CalculateNewPosition(transform.position, normalisedVelocity, normalisedAcceleration, t);
  32. Vector3 castHit;
  33. Vector3 norm;
  34. bool endOnNavmesh;
  35. if (navMesh.Linecast(last, next, out endOnNavmesh, out castHit, out norm))
  36. {
  37. // add rotation according to the angle in which the last point stands to the hit point on the ground times 1.5
  38. // get angle of last point to hit point
  39. Vector2 projectedEnd = new Vector2(castHit.x - projectionPoints[projectionPoints.Count - 1].x, castHit.z - projectionPoints[projectionPoints.Count - 1].z);
  40. // get angle of controller
  41. Vector2 controllerDirection = new Vector2(transform.forward.x, transform.forward.z);
  42. // get difference between those two angles to get actual offset independent of world space
  43. rotation = Vector2.SignedAngle(projectedEnd, controllerDirection) * 1.5F;
  44. projectionPoints.Add(castHit);
  45. normalisedHitPoint = norm;
  46. pointOnNavMesh = endOnNavmesh;
  47. return;
  48. }
  49. else
  50. {
  51. projectionPoints.Add(next);
  52. }
  53. last = next;
  54. }
  55. normalisedHitPoint = Vector3.up;
  56. pointOnNavMesh = false;
  57. }
  58. /// <summary>
  59. /// Limits the eulerAngle vector given as input in its z rotation to the second input given
  60. /// </summary>
  61. /// <param name="toBeLimited">eulerAngle vector that will be limited in z rotation</param>
  62. /// <param name="limit">limit in degrees of z rotation left and right</param>
  63. /// <returns>eulerAngle vector with limited z rotation</returns>
  64. protected Vector3 limitRotation(Vector3 toBeLimited, float limit)
  65. {
  66. if (toBeLimited.z < 360-limit && toBeLimited.z > 180)
  67. {
  68. toBeLimited.z = 360-limit;
  69. }
  70. else if (toBeLimited.z > limit && toBeLimited.z < 180)
  71. {
  72. toBeLimited.z = limit;
  73. }
  74. return toBeLimited;
  75. }
  76. }