Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Robert Carlsson · Feb 10, 2014 at 06:25 PM · raycastlinerendererraycasthitworldspacelocalspace

Raycasting, How to draw a line to the position of the rays end when nothing is hit

So the problem I've got is that I need to be able to draw a Line using line renderer to the end of the raycast when nothing is hit. Here is a code sample that might help showing what I'm looking for:

 Vector3 fwd = AttackPos.transform.forward;
 
             if(Physics.Raycast(AttackPos.transform.position, fwd, out hit, shotDistance)){
                 if(hit.transform.tag == "Other"){
                     hit.transform.GetComponent<HealthManager>().TookDamage(bulletDamage, hit.point);
                     if(hit.transform.gameObject.layer == 9){
                         _netMan.ShotPlayer(hit.transform.GetComponent<Interpolation>().gottenIndex, bulletDamage, hit.point);
                     }
                     else{
                         if(hit.transform.GetComponent<Interpolation>() != null){
                             _netMan.ShotAI(hit.transform.GetComponent<Interpolation>().gottenIndex, bulletDamage, hit.point);
                         }
                         else{
                             _netMan.ShotAI(_netMan.playerINDEX, bulletDamage, hit.point);
                         }
                     }
                     
                 }
                 if(hasLaserBeam){
 
                     DrawLine(linePos.transform.position, hit.point);
                 }
             }
             else{
                 if(hasLaserBeam){
                     DrawLine(linePos.transform.position, fwd*shotDistance);
                 }
             }


All the code inside the if(hit) statement works, it is tThe else statement ( when nothing is hit) that is the problem. It keeps drawing a line towards the origo of the scene instead of to the position fwd*shotDistance. The attackposition is a child object so can this problem be related to transformpoint/transformdirection since the coordinates aren't in worldspace?

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
4
Best Answer

Answer by robertbu · Feb 10, 2014 at 07:17 PM

The problem is that your DrawLine() takes two positions (assumed from the first call to DrawLine()). On line 26, you are passing it a position and a direction. The line should be:

 DrawLine(linePos.transform.position, AttackPos.transform.position+fwd*shotDistance);
Comment
Add comment · Show 2 · 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 sumeetkhobare · Feb 10, 2014 at 07:48 PM 0
Share

This is correct.. That's what I wanted to reply.. I can't plus one this.. low reputation .. SOrry..

avatar image Robert Carlsson · Feb 10, 2014 at 08:42 PM 0
Share

Silly me, of course. I knew I was overlooking something but just couldn't figure out what. Thanks for the quick and good response :)!

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

19 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

Related Questions

Drawing a line in front of player weapon 0 Answers

How can I can I cast a ray from a gameobject? 1 Answer

Applying Constant Force to Object A using Object B's Vectors. 0 Answers

Using raycast and collider to increase int 1 Answer

NullReference when checking tag via Raycast. How to solve this? 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