- Home /
GameObject detect LineRenderer colliding?
Hello,
First of all, here is a video for reference throughout the post. And here is my code.
In my project, I currently have a laser set up using LineRenderer. The laser will go on indefinitely. When a player or object collides with the laser, the laser stops at that collision point and has that spark effect turn on (as seen in the video).
On the opposite side of where the laser starts is a receiver point with a red indicator. I'm trying to make it so when the laser touches the white cube below the red indicator, the indicator will change to a green indicator. And of course, when the laser is not touching it, it changes to a red indicator. I have the code, but the cube can't detect when the laser is colliding with it. The laser stops when it collides with the cube, but the cube can't detect the laser is touching it and thus won't change the indicator.
I thought about adding a collision to the laser, but 1. there isn't any collision for line renderers, and 2. I still want to be able to allow the player/a cube to interrupt the laser.
So, I'm stuck on what to do next. Any help is appreciated. Thanks :)
Answer by lachlan_orton · Feb 22, 2019 at 10:27 AM
Instead of referencing a method in a seperate script, I introduced both red and green indicator gameobjects to the Laser script, so if the laser touches the end white block, it disables the red indicator and enables the green indicator. And if the laser isn't touching it, then the green indicator gets disabled and the red one is enabled.
using UnityEngine;
using System.Collections;
public class Laser : MonoBehaviour {
public GameObject sparks;
public GameObject redIndicator;
public GameObject greenIndicator;
private LineRenderer lr;
// Use this for initialization
void Start () {
lr = GetComponent<LineRenderer>();
}
// Update is called once per frame
void Update () {
lr.SetPosition(0, transform.position);
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.forward, out hit))
{
if (hit.collider)
{
lr.SetPosition(1, hit.point);
sparks.SetActive(true);
sparks.transform.position = hit.point;
greenIndicator.SetActive(false);
redIndicator.SetActive(true);
if (hit.collider.tag == "LaserEnd")
{
lr.SetPosition(1, hit.point);
sparks.SetActive(false);
redIndicator.SetActive(false);
greenIndicator.SetActive(true);
}
}
}
else
{
lr.SetPosition(1, transform.forward * 5000);
sparks.SetActive(false);
greenIndicator.SetActive(false);
redIndicator.SetActive(true);
}
}
}
Thanks for your help :D
Good you got it to work, I can see you used raycasts, where you check the collider.tag, like I said =)
Answer by SteenPetersen · Feb 21, 2019 at 07:58 AM
You definitely want to be using raycasts for this.
void Update()
{
// Bit shift the index of the layer (8) to get a bit mask
int layerMask = 1 << 8;
// This would cast rays only against colliders in layer 8.
// But instead we want to collide against everything except layer 8. The ~ operator does this, it inverts a bitmask.
layerMask = ~layerMask;
RaycastHit hit;
// Does the ray intersect any objects excluding the player layer
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, Mathf.Infinity, layerMask))
{
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * hit.distance, Color.yellow);
Debug.Log("Did Hit");
}
else
{
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * 1000, Color.white);
Debug.Log("Did not Hit");
}
}
Specially the part the says hit.distance is important for you as that will stop your laser on the interuptor and return a collision detection with which you can do as you please.
Without knowing exactly how you're shooting your laser I cant tell for sure, but it should be very similar to how you shoot your line renderer.
Where would I be implementing this code? In my laser script?
And is there anything I can share with you so you "know exactly how you're shooting your laser"
I'm new to C# so its a learning process for me atm. Thanks :)
Yea if your line renderer is shooting correctly as you want it to then it should be trivial to simply place a raycast alongside it as well. So if you paste your linerenderer code that should suffice.
The Code I linked is just pseudoCode btw to show how to shoot a ray in general, not applied to your case at all.
Linerender code is in a screenshot in the album linked above, but I'll paste the code here anyway:
using UnityEngine;
using System.Collections;
public class Laser : $$anonymous$$onoBehaviour {
public GameObject sparks;
private LineRenderer lr;
// Use this for initialization
void Start () {
lr = GetComponent<LineRenderer>();
}
// Update is called once per frame
void Update () {
lr.SetPosition(0, transform.position);
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.forward, out hit))
{
if (hit.collider)
{
lr.SetPosition(1, hit.point);
sparks.SetActive(true);
sparks.transform.position = hit.point;
}
}
else
{
lr.SetPosition(1, transform.forward * 5000);
sparks.SetActive(false);
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612192139im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Help with ontrigger enter and exit with UI Display appear and disappear 1 Answer
how to use right stc on gamepad like Input.mousePosition 0 Answers
instantiating unique classes of a type 0 Answers
[Solved] Help with a simple endless level generator. 1 Answer
Enabling multiple Monobehaviour Components in a game object 1 Answer