Mirror bouncing lasers

This commit is contained in:
Ava Gaiety Wroten 2020-06-15 17:49:35 -05:00
parent f2228b4182
commit b1ac62748b
3 changed files with 724 additions and 375 deletions

View file

@ -92,9 +92,9 @@ LineRenderer:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2115795504606128404}
m_Enabled: 1
m_CastShadows: 1
m_ReceiveShadows: 1
m_DynamicOccludee: 1
m_CastShadows: 0
m_ReceiveShadows: 0
m_DynamicOccludee: 0
m_MotionVectors: 0
m_LightProbeUsage: 0
m_ReflectionProbeUsage: 0
@ -102,7 +102,7 @@ LineRenderer:
m_RenderingLayerMask: 1
m_RendererPriority: 0
m_Materials:
- {fileID: 0}
- {fileID: 2100000, guid: 0244f7fe345a4904db014baf3a3d5034, type: 2}
m_StaticBatchInfo:
firstSubMesh: 0
subMeshCount: 0
@ -146,9 +146,9 @@ LineRenderer:
m_RotationOrder: 4
colorGradient:
serializedVersion: 2
key0: {r: 1, g: 1, b: 1, a: 1}
key1: {r: 1, g: 1, b: 1, a: 1}
key2: {r: 0, g: 0, b: 0, a: 0}
key0: {r: 0.16816717, g: 1, b: 0.031372547, a: 1}
key1: {r: 1, g: 0.8835529, b: 0.033018887, a: 1}
key2: {r: 1, g: 0.8835529, b: 0.033018887, a: 0}
key3: {r: 0, g: 0, b: 0, a: 0}
key4: {r: 0, g: 0, b: 0, a: 0}
key5: {r: 0, g: 0, b: 0, a: 0}
@ -156,7 +156,7 @@ LineRenderer:
key7: {r: 0, g: 0, b: 0, a: 0}
ctime0: 0
ctime1: 65535
ctime2: 0
ctime2: 65535
ctime3: 0
ctime4: 0
ctime5: 0
@ -176,7 +176,7 @@ LineRenderer:
numCornerVertices: 0
numCapVertices: 0
alignment: 0
textureMode: 0
textureMode: 1
shadowBias: 0.5
generateLightingData: 0
m_UseWorldSpace: 1
@ -209,3 +209,4 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
flipDirection: 0
power: 1

File diff suppressed because it is too large Load diff

View file

@ -20,55 +20,48 @@ public class Laser : MonoBehaviour
void Update()
{
RaycastHit2D hit = DrawLaser(transform.position);
HandleHit(hit);
DrawLaser(transform.position, isHorizontal, flipDirection);
}
RaycastHit2D DrawLaser(Vector3 startPosition)
void DrawLaser(Vector3 startPosition, bool drawOnYAxis, bool flipAxis, int laserIndex = 0)
{
laserLine.SetPosition(0, startPosition);
RaycastHit2D hit;
RaycastHit2D hit = DrawRaycast(startPosition, drawOnYAxis, flipAxis);
Vector3 endPosition;
if (isHorizontal)
{
float endpointY = flipDirection ? startPosition.y - maxDistance : startPosition.y + maxDistance;
hit = Physics2D.Raycast(startPosition, flipDirection ? Vector2.down : Vector2.up);
if (hit.collider) endPosition = hit.point;
else if (drawOnYAxis) endPosition = new Vector3(startPosition.x, flipAxis ? startPosition.y - maxDistance : startPosition.y + maxDistance, 0);
else endPosition = new Vector3(flipAxis ? startPosition.x - maxDistance : startPosition.x + maxDistance, startPosition.y, 0);
if (hit.collider) laserLine.SetPosition(1, hit.point);
else laserLine.SetPosition(1, new Vector3(startPosition.x, endpointY, 0));
} else
{
float endpointX = flipDirection ? startPosition.x - maxDistance : startPosition.x + maxDistance;
hit = Physics2D.Raycast(startPosition, flipDirection ? Vector2.left : Vector2.right);
laserLine.positionCount = laserIndex + 2;
laserLine.SetPosition(laserIndex, startPosition);
laserLine.SetPosition(laserIndex + 1, endPosition);
if (hit.collider) laserLine.SetPosition(1, hit.point);
else laserLine.SetPosition(1, new Vector3(endpointX, startPosition.y, 0));
}
return hit;
}
void HandleHit(RaycastHit2D hit)
{
if (hit.collider)
{
if (hit.collider.tag == "Mirror") HitMirror(hit);
if (hit.collider.tag == "Charge Point") HitChargePoint(hit);
if (hit.collider.tag == "Mirror")
{
Vector2 offset = GetDrawDirection(!drawOnYAxis, flipAxis);
DrawLaser(hit.point + offset, !drawOnYAxis, flipAxis, laserLine.positionCount);
} else
{
if (hit.collider.tag == "Charge Point") HitChargePoint(hit);
else UnchargeLastPoint();
}
}
else HitNothing();
else UnchargeLastPoint();
}
void HitMirror(RaycastHit2D hit)
RaycastHit2D DrawRaycast(Vector3 startPosition, bool drawOnYAxis, bool flipAxis)
{
return Physics2D.Raycast(startPosition, GetDrawDirection(drawOnYAxis, flipAxis));
}
if (isHorizontal)
{
// TODO
}
else
{
// TODO
}
Vector2 GetDrawDirection(bool drawOnYAxis, bool flipAxis)
{
if (drawOnYAxis && flipAxis) return Vector2.down;
else if (drawOnYAxis && !flipAxis) return Vector2.up;
else if (!drawOnYAxis && flipAxis) return Vector2.left;
return Vector2.right;
}
void HitChargePoint(RaycastHit2D hit)
@ -77,7 +70,7 @@ public class Laser : MonoBehaviour
lastChargePoint = hit.collider.gameObject;
}
void HitNothing()
void UnchargeLastPoint()
{
if (lastChargePoint) lastChargePoint.GetComponent<ChargePoint>().adjustCharges(power * -1);
lastChargePoint = null;