- Home /
LineRenderer Not working when parented
I am using a linerenderer for a laser sight on a weapon. It uses a raycast from the source of the laser, shooting out either thirty units or to anything obstructing it. When it has no parent, everything works correctly as it should. The line renders to the proper length and positions, but when I parent it to the gun, suddenly it malfunctions. The source of the laser is now somewhere above the gun and it is cast in a strange direction.
I have tried toggling the useWorldSpace boolean, but that hasn't helped.
Here is my code for the laser.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LaserSight : MonoBehaviour
{
public LayerMask detectionmask;
public float length = 30f;
public LineRenderer lineRenderer;
public GameObject surfaceFX;
RaycastHit hit;
Vector3[] positions = new Vector3[2];
// Update is called once per frame
void LateUpdate()
{
if (Physics.Raycast(transform.position, transform.forward, out hit, length, detectionmask))
{
positions[0] = transform.position;
positions[1] = hit.point;
lineRenderer.SetPositions(positions);
if (!surfaceFX.activeInHierarchy)
surfaceFX.SetActive(true);
surfaceFX.transform.position = hit.point;
}
else
{
positions[0] = transform.position;
positions[1] = transform.position + (transform.forward * length);
lineRenderer.SetPositions(positions);
if (surfaceFX.activeInHierarchy)
surfaceFX.SetActive(false);
}
}
}
It works when there is no parent, but as soon as it has a parent, the line goes crazy. Any help would be appreciated.
Answer by phouse2019 · Jan 12, 2019 at 07:24 PM
So it turns out that it was an issue with another script. I had a rig for a character that was animating and using LateUpdate to force the upperbody to track mousemovement. The laser (also using late update) was executing before the skeletal controls and thus its positions never matched the current positions and rotations of the bones on my character. So the ray was never rotating. I put that script first in the execution list and recomposed the laser script to perform better.
Here is the new script of the laser that works.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LaserSight : MonoBehaviour
{
public LayerMask detectionmask;
public float length = 30f;
public LineRenderer lineRenderer;
public GameObject surfaceFX;
RaycastHit hit;
Vector3[] positions = new Vector3[2] { Vector3.zero, Vector3.zero };
Vector3 noObstructPoint { get { return transform.position + (transform.forward * length); } }
/// <summary>
/// The ray point is the position where the laser strikes a surface or the end of the unobstructed laser. Use for tracking and positioning.
/// </summary>
[HideInInspector] public Vector3 rayPoint = Vector3.zero;
private void Start()
{
lineRenderer.useWorldSpace = true;
}
private void LateUpdate()
{
positions[0] = transform.position;
if (Physics.Raycast(transform.position, transform.forward, out hit, length, detectionmask))
{
positions[1] = hit.point;
if (!surfaceFX.activeInHierarchy)
surfaceFX.SetActive(true);
surfaceFX.transform.position = hit.point;
}
else
{
positions[1] = noObstructPoint;
if (surfaceFX.activeInHierarchy)
surfaceFX.SetActive(false);
}
rayPoint = positions[1];
lineRenderer.SetPositions(positions);
}
}