- Home /
Bouncing Raycast "Laser" Not Working?
What is wrong with this script? Whenever I run it I get no errors and it runs completely fine, but in game it only shows up as a single line and since it's only a single line I can't tell if it's actually working or not. My only issue with this script is that the lines aren't showing up correctly.
using UnityEngine;
using System.Collections;
public class Emission : MonoBehaviour {
public Vector3 position;
public static Vector3 staticPosition;
public Vector3 direction;
public static Vector3 staticDirection;
void Start () {
position = transform.position;
staticPosition = position;
direction = transform.TransformDirection (Vector3.forward);
staticDirection = direction;
}
void FixedUpdate () {
LineRenderer lineRenderer = GetComponent <LineRenderer>();
RaycastHit hit;
if (Physics.Raycast (position, direction, out hit)) {
lineRenderer.SetVertexCount (2);
lineRenderer.SetPosition (0, position);
lineRenderer.SetPosition (1, hit.point);
position = hit.point;
direction = Vector3.Reflect (position, hit.normal);
}
else {
position = staticPosition;
direction = staticDirection;
}
}
}
Also, when I use Debug.DrawLine it shows up as a line of the same length except it's constantly blinking. As said, My only issue with this script is that the lines aren't showing up correctly.
You are only assigning two vertexes to your line which will only ever give you a single line. To get a line that bounces you'll need a third.
I know that, but shouldn't the two vertexes constantly blink and change position creating a sort of bouncing pulsing line? If not, what do I assign the third vertex's Vector3 to?
Ah! Is see what you're trying to do now. $$anonymous$$isunderstood request and assumed you wanted a permanent laser effect.
Have you tried placing Debug.Log(position) at the end of the IF statement? It might reveal if its even being changed.
I'll try that, but since position is public I can view it in the editor and I believe it was changing, it was changing but I don't remember what they were, so I believe it's confirmed to be changing. I'll check in a bit when I get onto my computer.
Also, what way should I use more than one raycast because I think using more raycasts is the only way, the only what I can think of doing it is with an if-if statement, but how do I do that without having to infinifely write if statements inside of itself?
Answer by Hugs Are Drugs · Oct 16, 2012 at 02:05 PM
I found the issue, in Vector3.reflect () I put the inDirection as the hit.point of the raycast instead of the initial direction of the raycast, stupid me.
Your answer
Follow this Question
Related Questions
Linerenderer doesn't match my ray. 1 Answer
using A* pathfinding to go to player after raycast has seen it - c# 0 Answers
Raycast problem 1 Answer
Mouse Click + Raycast + Colliders 2 Answers