Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. void DrawCurrentTrajectory() {
  2. Vector2 position = transform.position;
  3. Vector2 direction = firePosition.position - transform.position;
  4.  
  5. RaycastHit2D hit = Physics2D.Raycast(position, direction, maximumRayCastDistance);
  6. Debug.DrawLine(position, position + direction * maximumRayCastDistance, Color.red);
  7.  
  8. if (hit) {
  9. Debug.DrawLine(position, hit.point, Color.green);
  10. Debug.DrawLine(hit.point, hit.point + hit.normal * 0.25f, Color.magenta);
  11.  
  12. position = hit.point + hit.normal * 0.00001f;
  13. }
  14. }
  15.  
  16. void DrawCurrentTrajectory() {
  17. Vector2 position = transform.position;
  18. Vector2 direction = firePosition.position - transform.position;
  19.  
  20. for(int i = 0; i <= maximumReflectionCount; ++i) {
  21. RaycastHit2D hit = Physics2D.Raycast(position, direction, maximumRayCastDistance);
  22.  
  23. if (hit) {
  24. Debug.DrawLine(position, hit.point, Color.green);
  25. Debug.DrawLine(hit.point, hit.point + hit.normal * 0.25f, Color.magenta);
  26.  
  27. position = hit.point + hit.normal * 0.00001f;
  28. direction = Vector2.Reflect(direction, hit.normal);
  29. }
  30. }
  31. }
  32.  
  33. public class RicochetTrajectory : MonoBehaviour
  34. {
  35. public Transform firePosition;
  36. public int maximumReflectionCount = 5;
  37. public float maximumRayCastDistance = 50f;
  38.  
  39. LineRenderer lineRenderer;
  40.  
  41. List<Vector3> reflectionPositions = new List<Vector3>();
  42.  
  43. private void Awake() {
  44. lineRenderer = GetComponent<LineRenderer>();
  45. lineRenderer.startWidth = 0.1f;
  46. lineRenderer.endWidth = 0.1f;
  47. }
  48.  
  49. void Update()
  50. {
  51. DrawCurrentTrajectory();
  52. }
  53.  
  54. void DrawCurrentTrajectory() {
  55. reflectionPositions.Clear();
  56.  
  57. Vector2 position = transform.position;
  58. Vector2 direction = firePosition.position - transform.position;
  59.  
  60. reflectionPositions.Add(position);
  61.  
  62. for(int i = 0; i <= maximumReflectionCount; ++i) {
  63. RaycastHit2D hit = Physics2D.Raycast(position, direction, maximumRayCastDistance);
  64. if (hit) {
  65. position = hit.point + hit.normal * 0.00001f;
  66. direction = Vector2.Reflect(direction, hit.normal);
  67.  
  68. reflectionPositions.Add(position);
  69. }
  70. }
  71.  
  72. lineRenderer.positionCount = reflectionPositions.Count;
  73. lineRenderer.SetPositions(reflectionPositions.ToArray());
  74. }
  75. }
  76.  
  77. position = hit.point + hit.normal * 0.00001f;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement