Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by belvita · Nov 29, 2013 at 10:28 AM · transformrayhitmouse cursorhitpoints

Bring ray to gameObject.y position when mouse cursor is on a different point

Hello,

I have a gameObject instantiated on the scene and using

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

in the Update function

when the mouse cursor is on the gameObject i get the objects coordinates that is fine and basic:

  if( Physics.Raycast( ray,out hit) )
     {
 

if(hit.collider.gameObject.tag=="sandcube")

{ sandcubePos=hit.collider.gameObject.transform.position;

                                                                                         }

....... this is fine

now my mouse cursor is not on the gameObject and i press x button then i want the ray to be available from only the first collided gameObjects.y position which is: sandcubePos.y so what i do is:

if(Input.GetKey(KeyCode.X)) {

ray=Camera.main.ScreenPointToRay(new Vector3(Input.mousePosition.x,sandcubePos.y,0));

but it doesnt work but supposed to!

if i write: ray=Camera.main.ScreenPointToRay(new Vector3(Input.mousePosition.x,sandcubePos.y+300,0));

now the ray is quite close to the first collided gameObject when the mouse cursor is on a different point but it changes all the time depending on the collided gameObject!!

the code is attached to first person controller and the ray keeps changing

how to lock this mouse ray to only the collided game object with the mouse cursor collider(hit.point) no matter where my fpc object ,strange!!!

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
-1

Answer by robertbu · Nov 29, 2013 at 05:42 PM

Referring to:

ray=Camera.main.ScreenPointToRay(new Vector3(Input.mousePosition.x,sandcubePos.y,0));

You cannot use this calculation because you are mixing screen coordinates and world coordinates. Input.mousePosition is in screen coordinates (pixels), and sandcubePos is in world coordinates. What you can do is save the Input.mousePosition.y from the previous raycast and use that for constructing this ray.

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 belvita · Nov 29, 2013 at 07:33 PM 0
Share

i have already tried that wont work because the objects keeps moving anyway hope will find a solution soon

avatar image robertbu · Nov 29, 2013 at 08:04 PM 0
Share

Well, you cannot mix screen and world coordinates. How you fix things will depend on how things are moving and what you expect this cast to do. One thing that might work is to convert your sandcubePos back to a screen position, then use the 'y':

 Vector3 v3 = Camera.main.WorldToScreenPoint(sandcubePos);
 ray = Camera.main.ScreenPointToRay(new Vector3(Input.mousePosition.x, v3.y));

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

16 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

Related Questions

Calculate how far a ray has intercepted an object. 1 Answer

How do I add a force on the final shot with a gun? 0 Answers

Distance Variable Not Returning Value/Ray not casting 1 Answer

how to get info on what raycast hitted 1 Answer

Trouble with Ray & Raycasting and AddForce() 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