- Home /
Reflecting linerenderer with raycast?
Hi, im trying to make a "laser" reflect against objects. But I don't know how i can make it to change direction when It's hitting something. When I try to change the direction of the "laser" It changes it at the Startposition and not at the Hit position.
This is my code so far. using UnityEngine; using System.Collections;
public class testLaserScript : MonoBehaviour {
LineRenderer lineRenderer;
void Awake () {
lineRenderer = GetComponent<LineRenderer> ();
}
void Start(){
}
void Update () {
Laser ();
}
void Laser(){
RaycastHit hit;
if(Physics.Raycast(transform.position,Vector3.right,out hit,Mathf.Infinity)){
lineRenderer.enabled = true;
lineRenderer.SetPosition(0, transform.position);
lineRenderer.SetPosition(1, hit.point);
}
}
}
Answer by ChrisJoosten · Mar 06, 2014 at 12:53 PM
I got a little progression
using UnityEngine;
using System.Collections;
public class testLaserScript : MonoBehaviour {
LineRenderer lineRenderer;
void Awake () {
lineRenderer = GetComponent<LineRenderer> ();
}
void Start(){
}
void Update () {
Laser ();
}
void Laser(){
RaycastHit hit;
if(Physics.Raycast(transform.position,Vector3.forward,out hit,Mathf.Infinity)){
lineRenderer.enabled = true;
lineRenderer.SetPosition(0, transform.position);
lineRenderer.SetPosition(1, hit.point);
if(hit.collider.tag == "Mirrors"){
Vector3 pos = Vector3.Reflect (hit.point - this.transform.position, hit.normal);
lineRenderer.SetPosition(2,pos);
//lineRenderer.SetPosition(3, pos);
}
}
}
}
With this code it reflects against 1 object with the tag "Mirrors". No i need to make something it reflects against mutliple objects. It has to be dynamicle so when i remove a "Mirrors" object from the scene It doesn't bug of give an error.
Thanks in regards!
I am trying to do the same thing. When I use your code the laser gets smaller close to the mirrors and grows again as soon as it departs in another direction. Any ideas why?
Answer by CatsPawGames · Sep 10, 2014 at 03:55 AM
This package contains ray visualization including complex reflections using colliders:
Your answer
Follow this Question
Related Questions
How can I can I cast a ray from a gameobject? 1 Answer
Line Renderer Issues 1 Answer
Setting Line Render position directly forward from facing of game object 1 Answer
Shorten Raycast - Line Renderer 3 Answers
How would I set the end of my line renderer to the first collision point with a raycast? 0 Answers