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 givaway · Apr 25, 2012 at 05:21 AM · raycastscreentoworldpointmouse position

ScreenToWorldPoint has some sort of offset

Ive been trying to get this working for a while but i just cant figure it out. I have an object in my scene that has a script attached to it that raycast from its position to the mouse position. however if i move the object from its initail position the raycast seems to have an offset of some sort. (Images Below) alt text

Script (Attached to Cube) Here:

 var AttachedCamera:Camera;
 function Start () {
     
 }
 
 function Update () 
 {
     var MousePosition = this.AttachedCamera.ScreenToWorldPoint(Vector3(Input.mousePosition.x,Input.mousePosition.y,15));
     var hit:RaycastHit;
     if (Physics.Raycast (transform.position, MousePosition,hit, 20)) 
     {
         Debug.DrawLine(this.transform.position, hit.point, Color.red);
         Debug.Log(hit.point.x+","+hit.point.y);
     }
    
         Debug.DrawLine(this.transform.position, MousePosition, Color.blue);
    
     
     
 }

How do i get rid of this offset? thanks in advace

problem.png (29.3 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 keld-oelykke · Apr 25, 2012 at 07:44 PM 0
Share

Not sure I get the purpose of raycasting from box to camera...

$$anonymous$$aybe you would rather want to cast the opposite direction e.g. using http://unity3d.com/support/documentation/ScriptReference/Camera.ScreenPointToRay.html or http://unity3d.com/support/documentation/ScriptReference/Camera.ViewportPointToRay.html ?

avatar image keld-oelykke · Apr 25, 2012 at 07:58 PM 0
Share

I think I get your setup now ... 3 objects: - cube sending out ray (you want to check if ray hits plane) - plane above cube (can be hit by ray, if user point correctly) - camera - viewpoint of user and the place to get mouse position transformed into world from

If it is your idea that the ray from cube to plane is like pointing a canon... then rotate the ray around the cube using inputs.

If it is your idea that the ray from cube to plane needs to be visualized when you click at plane.... then you ray cast from camera to plane ins$$anonymous$$d. If it hits, you draw the line from cube to plane hitpoint.

Hope it makes sense.

1 Reply

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

Answer by Berenger · Apr 25, 2012 at 07:47 PM

The second parameter of that overload of Raycast is a direction, not a position. If transform.position is (0,0,0), it might trick you in believing it works. Try :

 if( Physics.Raycast(transform.position, MousePosition - transform.position, hit, 20) ) 
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 keld-oelykke · Apr 25, 2012 at 08:05 PM 0
Share

good point

avatar image Turcca · May 16, 2013 at 07:46 AM 0
Share

Ahh thanks, this helped me as well. (can't upvote for some reason)

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

6 People are following this question.

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

Related Questions

Camera.ScreenToWorldPoint with Perspective camera 1 Answer

Mouse pos + raycast 1 Answer

Contextual Rose Menu not determining correct context. 2 Answers

[SOLVED] Raycast only hits one side of the box collider 1 Answer

Error with gun aim to the mouse 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