-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLaserCast.cs
134 lines (110 loc) · 4.25 KB
/
LaserCast.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using UnityEngine;
// * Code is messy and repetative, but it works.
public class LaserCast : MonoBehaviour
{
public string target;
public string obsticle;
public Transform laser;
public Transform rayPoint;
public float rayPointOffset;
public float raycastDistance;
public Vector2 origin;
public Vector2 destination;
public Vector2 direction;
bool isX;
Vector2 gizmos;
void Start()
{
if (direction.x == 0) isX = false;
else isX = true;
}
void Update()
{
// Casts a ray at a specific direction
RaycastHit2D[] ray = Physics2D.RaycastAll(origin, direction, raycastDistance);
bool hasHitObsticle = false;
float obsticleLength = 0f;
bool hasHit = false;
foreach (RaycastHit2D rayHit in ray)
{
// If the laser has hit the obsticle before hitting Bonine, we simply stop the laser from going through
if (hasHitObsticle) break;
// IF the raycast collides with an obsticle or Bonine, script resizes the laser
if (rayHit.collider.CompareTag(target) || rayHit.collider.CompareTag(obsticle))
{
Vector2 center = (origin + rayHit.point) / 2;
gizmos = rayHit.point;
if (isX)
{
float lengthX = Mathf.Abs(rayHit.point.x - origin.x);
laser.localScale = new Vector2(lengthX, laser.localScale.y);
rayPoint.position = new Vector2(rayHit.point.x + rayPointOffset, rayPoint.position.y);
laser.position = center;
}
else
{
float lengthY = Mathf.Abs(rayHit.point.y - origin.y);
laser.localScale = new Vector2(laser.localScale.x, lengthY);
rayPoint.position = new Vector2(rayPoint.position.x, rayHit.point.y + rayPointOffset);
laser.position = center;
}
hasHit = true;
}
if (rayHit.collider.CompareTag(obsticle))
{
hasHitObsticle = true;
obsticleLength = rayHit.distance;
}
}
foreach (RaycastHit2D rayHit in ray)
{
if (!hasHitObsticle) break;
if (rayHit.collider.CompareTag(target))
{
if (rayHit.distance < obsticleLength)
{
Vector2 center = (origin + rayHit.point) / 2;
gizmos = rayHit.point;
if (isX)
{
float lengthX = Mathf.Abs(rayHit.point.x - origin.x);
laser.localScale = new Vector2(lengthX, laser.localScale.y);
rayPoint.position = new Vector2(rayHit.point.x + rayPointOffset, rayPoint.position.y);
laser.position = center;
}
else
{
float lengthY = Mathf.Abs(rayHit.point.y - origin.y);
laser.localScale = new Vector2(laser.localScale.x, lengthY);
rayPoint.position = new Vector2(rayPoint.position.x, rayHit.point.y + rayPointOffset);
laser.position = center;
}
hasHit = true;
}
}
}
if (!hasHit)
{
Vector2 center = (origin + destination) / 2;
if (isX)
{
float lengthX = Mathf.Abs(destination.x - origin.x);
laser.localScale = new Vector2(lengthX, laser.localScale.y);
rayPoint.position = new Vector2(destination.x + rayPointOffset, rayPoint.position.y);
laser.position = center;
}
else
{
float lengthY = Mathf.Abs(destination.y - origin.y);
laser.localScale = new Vector2(laser.localScale.x, lengthY);
rayPoint.position = new Vector2(rayPoint.position.x, destination.y + rayPointOffset);
laser.position = center;
}
}
}
void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawLine(origin, gizmos);
}
}