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 huggz · Feb 19, 2011 at 04:24 PM · raycastingmouseclick

Click Point From Raycasting Not Yielding Expected Coordinates

I'm working on a board game and have a textured rectangular solid thats 918 x 50 x 550, where 918 is the width of the board and 550 is the height. The board's texture is a 918 x 550 BMP. Then the main camera is looking down at a 63 degree angle. On update, I'm running a raycast to get the mouse click position and translating it to the texture coords:

private RaycastHit GetRaycast(Vector3 position) { Camera clickCamera = Camera.main;

 if(clickCamera == null)
     return default(RaycastHit);

 RaycastHit hit;
 Ray ray = clickCamera.ScreenPointToRay(position);

 if (!Physics.Raycast(ray, out hit)) 
     return default(RaycastHit);

 Renderer renderer = hit.collider.renderer;
 MeshCollider meshCollider = hit.collider as MeshCollider;

 if (renderer == null || renderer.sharedMaterial == null ||
     renderer.sharedMaterial.mainTexture == null || meshCollider == null)
     return default(RaycastHit);

 return hit;

}

private Vector2 GetHitCoordinatesOnObject(RaycastHit hit) { var pixelUV = hit.textureCoord; Texture2D tex = hit.collider.renderer.material.mainTexture as Texture2D;

 pixelUV.x *= tex.width;
 pixelUV.y *= tex.height;

 Debug.Log("clicked (" + pixelUV.x + "," + pixelUV.y + ")");

 return pixelUV;

}

However my debug message is displaying a range ~0 to ~1020 for the width and ~0 to ~510 on the height. This is throwing me off when I try to map the click to world coordinates. Can someone explain to me what's happening here and how I'm supposed to fix it?

Thank you very much!

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

Answer by Jessy · Feb 19, 2011 at 06:17 PM

I'm not sure I understand this completely, but I think what you're saying is that you expect values between 918 and 550 instead of 1024 to 512. A compressed texture has to be in powers of two. I figure your solution would be to not multiply by the dimensions of the texture, but rather, the values you actually want to use. Let me know if I'm off on this.

But I don't see why you need to use the UVs, anyway.

Comment
Add comment · Show 3 · 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 huggz · Feb 19, 2011 at 07:19 PM 0
Share

Because I have no clue what I'm doing with this. haha :) Different recommendations are more than welcome. This is my first Unity project. Thank you for you answer.

avatar image huggz · Feb 19, 2011 at 07:54 PM 0
Share

Yea, most of that code was from an example I found on the Unify wiki. I've modified it and am now using RaycastHit.point. Definitely a more elegant solution than the other. Thanks for your help.

avatar image Jessy · Feb 19, 2011 at 07:59 PM 0
Share

It seems to me that RaycastHit.point should work fine, and yield the same numbers you were going for with your textureCoord method. Did you have issues with it that caused you to switch the data checked?

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

No one has followed this question yet.

Related Questions

Use raycast to click on object 3 Answers

question about instantiating 2 Answers

How to destroy specific gameobject with a mouse click once player is looking at them using a Ray 1 Answer

I am stuck and don't know what to do, After upgrading to 2019 mouse and raycast don't work well 1 Answer

How to create gameobjects by clicking in the scene view in the editor? 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