Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by phouse2019 · Jan 12, 2019 at 02:03 PM · gamelinerendererrenderlaser

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);
     }
 }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

134 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

LineRenderer not working 0 Answers

Laser Render Reflect Angle incorrect 1 Answer

LineRendering Forward 1 Answer

LineRenderer (Laser Beam) is not following the ray it's going on the wrong direction when reflecting 1 Answer

Health Bar to Render On Top 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges