Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Lishin · Oct 30, 2016 at 07:13 PM · collisionraycastlinerendererlinetroubleshooting

LineRenderer. Problem with ray displaying

Hello. What am I doing wrong? Here is the code and screenshots with my problem( When the beam hits the object, it is reduced. P.S. Im not use WorldSpace coordinate

 public class Emitter : MonoBehaviour {
 
     private LineRenderer lr;
 
 
     void Start ()
     {
         lr = GetComponent<LineRenderer>();
     }
     
     void Update ()
     {
         RaycastHit hit;
         if (Physics.Raycast(transform.position, transform.forward, out hit))
         {
             if (hit.collider)
             {
                 lr.SetPosition(1, new Vector3(0, 0, hit.distance));
                 print("Hit: " + hit.distance);
             }
         }
         else
         {
             lr.SetPosition(1, new Vector3(0, 0, 100));
         }
     }
 
 }

alt text

1.jpg (77.2 kB)
2.jpg (77.2 kB)
Comment
Add comment · Show 2
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
avatar image Sergio7888 · Oct 30, 2016 at 07:43 PM 1
Share

if the LineRenderer is using WorldSpace replace lr.SetPosition(1, new Vector3(0, 0, hit.distance)); with lr.SetPosition(1, hit.point);.
Else replace lr.SetPosition(1, new Vector3(0, 0, hit.distance)); with lr.SetPosition(1, lr.transform.InverseTransformPoint(hit.point));.

avatar image Lishin Sergio7888 · Oct 30, 2016 at 07:51 PM 0
Share

Thanks for the tip, but I forgot to say that do not use WorldSpace.

2 Replies

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

Answer by Fliperamma · Oct 30, 2016 at 08:43 PM

Since you are not using WorldSpace in Line Renderer, you'll need to convert the end position/distance to the Local Space. Something like this:

 float dist = transform.InverseTransformVector(transform.position - hit.point).magnitude;
 lr.SetPosition(1, new Vector3(0, 0, dist));
Comment
Add comment · Show 1 · 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
avatar image Lishin · Oct 31, 2016 at 06:41 AM 0
Share

Thank you. Works great! But it is strange. I looked and read a lot of lessons and everywhere works as I did in the beginning. $$anonymous$$aybe the information is outdated. Thanks again!

avatar image
0

Answer by Lishin · Oct 30, 2016 at 08:21 PM

Update: If i use debug.drawray - hit.distance work perfectly

       if (Physics.Raycast(transform.position, transform.forward, out hit))
             {
                 Vector3 forward = transform.TransformDirection(Vector3.forward) * hit.distance;
                 Debug.DrawRay(transform.position, forward, Color.green);
 ...

 

alt text


3.jpg (126.2 kB)
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

95 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

Related Questions

How to draw a line between objects and get distance 1 Answer

How can I make raycast touch responsive with 2d world objects? Raycast misses the collider when finger is moved at faster rate. 0 Answers

Creating a Ray Gun 1 Answer

Line Collision Detection for 2D Game 1 Answer

Melee Attack - Kill Enemy on Contact with a ray 0 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