- Home /
Question by
MattBee · Mar 11, 2015 at 08:27 PM ·
raycastlinerenderer
Using line renderer with 2D Raycasts
Hi, I'm currently making a puzzle in which light bounces off of mirrors and has to reach an end point to finish the level. I have the raycasting sorted, it'll bounce off of the required object and reflect appropriately. I can't figure out how to implement the LineRenderer alongside my code.
Here's the class I'm using for the raycasting(laser)
using UnityEngine;
using System.Collections;
public class LightRefraction : MonoBehaviour
{
//private LineRenderer lineRenderer;
// Use this for initialization
void Start ()
{
//lineRenderer = GetComponent<LineRenderer>();
}
// Update is called once per frame
void Update ()
{
Laser();
}
public void Laser()
{
Debug.DrawRay(transform.position, -transform.up * 50, Color.black);
RaycastHit2D hit = Physics2D.Raycast(transform.position, -transform.up, Mathf.Infinity);
if( hit != null && hit.collider != null)
{
Vector3 inDirection = Vector3.Reflect(-transform.up, hit.normal);
Debug.DrawRay(hit.point, inDirection * 100, Color.red);
hit = Physics2D.Raycast(hit.point + hit.normal * 0.01f, inDirection, Mathf.Infinity);
if (hit != null && hit.collider != null)
{
inDirection = Vector3.Reflect(inDirection, hit.normal);
Debug.DrawRay(hit.point, inDirection * 100, Color.green);
hit = Physics2D.Raycast(hit.point + hit.normal * 0.01f, inDirection, Mathf.Infinity);
if(hit != null && hit.collider != null)
{
inDirection = Vector3.Reflect(inDirection, hit.normal);
Debug.DrawRay(hit.point, inDirection * 100, Color.blue);
}
}
}
}
}
Any help with setting up the LineRenderer to draw the lines the Debug.DrawRay() is drawing would be much appreciated.
Thanks, Matthew
Comment