2D Mirror system

 avatar
unknown
csharp
10 days ago
3.3 kB
11
Indexable
///////Miror.cs////// -> Attach to my grid
using UnityEngine;

public class Mirror : MonoBehaviour
{
    public void Propagate(LaserBeam laserBeam)
    {
        Vector2 outGoingDirection = Vector2.Reflect(laserBeam.Direction, laserBeam.hitNormal);
        laserBeam.Propagate(laserBeam.endPosition, outGoingDirection);
    }
}


//////LaserBeam.cs////// -> Attach to my laserBeam prefab 
using Needle.Console;
using UnityEngine;

public class LaserBeam : MonoBehaviour
{
    [SerializeField][Range(0f,20f)] private float beamMaxDistance = 9f;
    [SerializeField] private LayerMask wallLayer;
    [SerializeField] private int maxCollisionNumber;

    private Vector2 startPosition;
    [HideInInspector] public Vector2 endPosition;
    [HideInInspector] public Vector2 hitNormal = Vector2.zero;
    [HideInInspector] public Vector2 Direction;

    private RaycastHit2D hit;

    private Mirror mirrorTheBeamHit;

    [SerializeField] private int collisionNumber = 0;

    public void Propagate(Vector2 sourceTransform, Vector2 direction)
    {
        Debug.DrawLine(startPosition, endPosition, Color.red);
        if (collisionNumber < maxCollisionNumber)
        {
            collisionNumber++;
            endPosition = sourceTransform + direction * beamMaxDistance;
            hitNormal = Vector2.zero;
            hit = Physics2D.Raycast(sourceTransform, direction, beamMaxDistance, wallLayer);

            //Check if the laser beam hits something
            if (hit.collider != null)
            {
                endPosition = hit.point;
                hitNormal = hit.normal;
                mirrorTheBeamHit = hit.collider.GetComponent<Mirror>();
                Direction = (endPosition - sourceTransform).normalized;
            }
            else
            {
                Destroy(gameObject);
            }
            startPosition = sourceTransform;
            
            if(mirrorTheBeamHit != null)
            {
                DebugEditor.Log("Mirror hit", this);
                mirrorTheBeamHit.Propagate(this);
            }
            
        }
        else
        {
            Destroy(gameObject);
            //StartCoroutine(WaitBeforeDestroy());
        }
        
    }
}

//////LaserSource///// -> Attach to my player 
using UnityEngine;

public class LaserSource : MonoBehaviour
{
    [SerializeField] private Transform sourceTransform;
    [SerializeField] private GameObject laserBeamPrefab;

    private LaserBeam laserBeam;

    [ContextMenu("CreateLaser")]
    public void CreateLaser()
    {
        for (int i = 0; i < 11; i++) 
        {
            PropagateLaser(Quaternion.Euler(0, 0, i * 36));
        }
        PropagateLaser(Quaternion.Euler(0, 0, 90));
    }

    private void PropagateLaser(Quaternion rotationAngle)
    {
        //Propagate the laser beam from the sourceTransform position in the direction of the sourceTransform forward vector
        laserBeam = Instantiate(laserBeamPrefab, sourceTransform.position, Quaternion.identity, this.transform).GetComponent<LaserBeam>();
        sourceTransform.rotation = rotationAngle;
        laserBeam.Propagate(sourceTransform.position, (Vector2)sourceTransform.right);
    }
}
Editor is loading...
Leave a Comment