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 ReasondComb99 · Oct 04, 2016 at 03:01 AM · particleslinerendererlaser

How to stop linerender when it hits an object

I'm making a FPS and am trying to give my gun a laser sight(so a laser pointer basically) and am having trouble with my script as the laser still shines trough walls and does not stop like i want it to.

function Update () {
    var LaserSight : GameObject;
    var hit : RaycastHit;
    Physics.Raycast(transform.position, transform.forward, hit);
    if (hit.collider) {
        LaserSight.SetPosition(1, Vector3(0,0,hit.distance));
    }
    else {
        LaserSight.SetPosition(1, Vector3(0,0, 5000));
    }
}

I also tried this code

 function Update () {
     var lineRenderer : LineRenderer = GetComponent(LineRenderer);
     lineRenderer.useWorldSpace = false;
     lineRenderer.SetVertexCount(2);
     var hit : RaycastHit;
     Physics.Raycast(transform.position,transform.forward,hit);
     if(hit.collider){
     lineRenderer.SetPosition(1,Vector3(0,0,hit.distance));
     }
     else{
         lineRenderer.SetPosition(1,Vector3(0,0,5000));
     }
 }
 
 
 @script RequireComponent(LineRenderer)

Comment
Add comment · Show 1
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 chillersanim · Oct 05, 2016 at 08:50 PM 0
Share

Why do you use if(hit.collider) ins$$anonymous$$d of if (Physiccs.Raycast(...)) ?
Also the RaycastHit argument should have the out keyword.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by doublemax · Oct 05, 2016 at 05:23 AM

The endposition of the line renderer looks strange in both cases. Also: don't forget to set the start positon of the line renderer.

Try something like this:

 lineRenderer.SetPosition(0, transform.position );
 if(hit.collider){
  lineRenderer.SetPosition(1, hit.point);
 }
 else {
  lineRenderer.SetPosition(1, transform.position + transform.forward * 100.0f );
 }
Comment
Add comment · Show 8 · 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 ScaniX · Oct 05, 2016 at 08:50 AM 1
Share

It is set to useWorldspace = false, so I guess start position is 0,0,0 always. I guess hit.distance needs to be scaled to world space to make it work (the gun probably has a scale). Or he could switch to worldSpaceCoords and use the above code. :)

avatar image doublemax ScaniX · Oct 05, 2016 at 09:51 AM 0
Share

I totally missed that. Thanks for noticing.

avatar image ReasondComb99 doublemax · Oct 05, 2016 at 08:44 PM 0
Share

I tried but this happened: alt text

unityproblem.png (207.9 kB)
Show more comments
Show more comments
avatar image
0

Answer by GuruJeya14 · Feb 02, 2019 at 08:20 AM

(I know this conversation occurred long time ago. For people who are in search of the same question....)

Try using Physics.Linecast for our line renderer, instead of Physics.Raycast.

Also check this out, it is explained well by @tsnyder321

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

7 People are following this question.

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

Related Questions

LineRenderer not working 0 Answers

LineRenderer Not working when parented 1 Answer

Lightaber effect? 0 Answers

LineRendering Forward 1 Answer

LineRenderer or Particles 1 Answer


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