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 nickostan · Sep 04, 2017 at 09:53 PM · raycastbuglinerendererbug-perhaps

Raycasting is locking to a certain position (is this a bug?)

Hi all,

I have a script that basically says

When RIGHT CLICK IS HELD, RAYCAST in the direction the mouse is pointing to a distance of 10.0F UNITS. IF something is hit, RENDER A LINE between the ORIGIN and the HIT POINT. IF something is NOT HIT, RENDER A LINE between the ORIGIN and the END OF THE RAY.

For some reason, when my ray doesn't hit anything, the line is rendered between the origin and a seemingly far away point. The strange thing is, I have a red Debug.DrawLine appear in order to show me the direction and magnitude of the raycast, and it seems like it's always casting upwards? See gif below.

alt text

Here is my code:

  void Update () {
         //RIGHT CLICK CHECK
         if (Input.GetMouseButton(1))
         {
             Ray ray = viewCamera.ScreenPointToRay(Input.mousePosition);
             Vector3 clickPoint = ray.origin;
             clickPoint.z = 0;
             CmdNawlinsLazer(clickPoint);
         }
 
     }
 
 void CmdNawlinsLazer(Vector3 clickpoint)
     {
 
         RaycastHit2D hit = Physics2D.Raycast(gunTip.position, clickpoint - gunTip.position, 10.0F, whatLazerHits);
         Debug.DrawLine(gunTip.position, clickpoint - gunTip.position * 10, Color.red, 0.5F);
 
         print(hit.collider);
 
         if(hit.collider == null)
         {
             print("NOT HITTING ANYTHING, RENDER LINE TO END OF RAY which is " + clickpoint - gunTip.position * 10);
             //Enable the line renderer. Set two points. Render the line.
             //_lineRenderer.enabled = true;
             _lineRenderer.positionCount = 2;
             Vector3[] points = new Vector3[2];
             points[0] = gunTip.transform.position;
             points[1] = clickpoint - gunTip.position * 10;
             _lineRenderer.startWidth = 0.05F;
             _lineRenderer.endWidth = 0.05F;
             _lineRenderer.alignment = LineAlignment.View;
             _lineRenderer.SetPositions(points);
 
             //print(points[0] + " and " + points[1]);
         }
         else
         {
             print("HITTING SOMETHING AT" + hit.point);
             _lineRenderer.positionCount = 2;
             Vector3[] points = new Vector3[2];
             points[0] = gunTip.transform.position;
             points[1] = hit.point;
             _lineRenderer.startWidth = 0.05F;
             _lineRenderer.endWidth = 0.05F;
             _lineRenderer.alignment = LineAlignment.View;
             _lineRenderer.SetPositions(points);
         }
     }

I've seen this now in two of my scripts, so I think it may be a bug? I've been confused on this for about 4 days now. Hoping I can get some help here.

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 Glurth · Sep 04, 2017 at 10:15 PM

  clickpoint - gunTip.position 

While this expression IS correct for defining a Ray's direction:

  Physics2D.Raycast(gunTip.position, clickpoint - gunTip.position, 10.0F, whatLazerHits);

It is not the correct expression for the line renderer: that expression should be the actual location you want to draw to (`clickpoint`). If you want the line 10 times the distance of click point away:

 offset =  (clickpoint - gunTip.position) * 10;  //PARENS here are *critical*
 endpoint = clickpoint + offset;
 points[1] = endpoint ;
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 nickostan · Sep 04, 2017 at 10:22 PM 0
Share

Dude, you're awesome. Thank you so much. Works beautifully. Now I'm going to read this over to try to understand this.

Cheers!

avatar image Glurth nickostan · Sep 04, 2017 at 10:32 PM 0
Share

" try to understand " things to keep in $$anonymous$$d, study: A Vector CAN represent a position in 2d or 3d space. It can also represent an OFFSET (translation) to a position. In this form we are using the vectors direction and the magnitude(length). Offset vectors are generally only useful when used in conjunction with a position vector.
An offset vector and a position vector, are basically the same EXACT thing: except that the "position" vector is ALWAYS used as an "offset" from the ORIGIN (0,0,0). This is why we can subtract one "position" from another "position", to get an "offset".

"Order of operations": you need to know these like the back of your hand. (why parens are needed).

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

99 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

Related Questions

Question about raycasting and UI button 0 Answers

Unity Line Renderer drawing extra line to another point.. 0 Answers

Shorten Raycast - Line Renderer 3 Answers

Linerender/Raycast2D colliding inconsistently with sprites 0 Answers

LineRenderer objects are clipped/culled early 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