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 Saripsis · Oct 24, 2016 at 03:12 AM · raycastscreenpointtoray

Raycast not calculating correctly (doesn't go to mouse position)

The raycast coming from the gameobject and going to the hit point is not going to the position of the mouse. The linerenderer works as it should. alt text

PlayerController Script

         if(Input.GetButton("Laser")){
             
             RaycastHit hit;
             Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
             if (Physics.Raycast (ray, out hit)) {
                 mousePoint = hit.point;
             } else {
                 mousePoint = new Vector3 (emitter.transform.position.x, emitter.transform.position.y, 100f);
                 print ("no mousepoint");
             }
 
             Debug.DrawRay (emitter.transform.position, mousePoint, Color.red);
 
             Physics.Raycast(emitter.transform.position, mousePoint, out targetHit); 
             if (targetHit.collider != null) {
                 target = targetHit.collider.gameObject;
                 print (target.gameObject.name);
             } 
 
         
         }//end laser

LineRenderer Script

     // Use this for initialization
     void Start () {
 
         lineRenderer = GetComponent<LineRenderer> ();
         player = GameObject.FindGameObjectWithTag ("Player");
 
 
         lineRenderer.SetPosition (0, emitter.transform.position);
         lineRenderer.SetWidth (.05f, .05f);
 
 
     }
     
     // Update is called once per frame
     void Update () {
         Vector3 mousePoint = player.GetComponent<PlayerController> ().mousePoint;
 
         lineRenderer.SetPosition (1, mousePoint);
 
     }
 }


untitled.png (201.3 kB)
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

2 Replies

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

Answer by Bunny83 · Oct 24, 2016 at 05:13 AM

DrawRay takes one worldspace position and a worldspace direction. However you pass two positions. So either use DrawLine which takes two positions or pass the ray direction as second parameter.

To be more precise you might also want to use they ray's origin as that's the actual starting point of the ray:

 Debug.DrawRay(ray.origin, ray.direction * 5f, Color.red);

Note: the direction is usually a normalized direction (a vector with length 1.0). So you have to multiply the direction with your desired length when you visualize it with DrawRay.

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
avatar image
0

Answer by Rubiix · Sep 21, 2020 at 03:47 PM

To anyone else having this issue, this answer was very useful!

I switched from using RayCast as above to using

RaycastHit2D hit = Physics2D.Linecast(origin, direction, whatToHit);

And it works smoothly now, Thanks Bunny!

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

96 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

Related Questions

camera.ScreenPointToRay always has same origin... 1 Answer

Not getting right value; ScreenPointToRay 0 Answers

Scripting a Click event in the Unity Editor 1 Answer

Problem with anims when attacking 1 Answer

Raycast hit done wrong using ScreenPointToRay coordinates 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