Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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
0
Question by Guy-Corbett · Jul 20, 2018 at 01:30 PM · raycastraycasthituvuv coordinatestexturecoord

RaycastHit.textureCoord ranges from (0,0) to (1,1) for each face.

Hi all,

I have a simple cube, with a material applied. I am casting a ray at it from mouse click positions and looking at the value of textureCoord in the RaycastHit structure. No matter which side of my cube I click, texture coord always gets a value of (0, 0) if I click near the bottom left, and (1, 1) near the top right. It's reporting the coordinate I clicked within the texture on that side of the cube, not the texture as a whole. Does anyone know why it's doing this? Or how I can stop it? To clarify;

The texture on my cube is actually a RenderTexture being captured from a camera elsewhere in my scene. That camera's preview looks like this;

alt text

And I have the UVs on my cube set up so that each one of those numbers in the texture maps nicely to one side of the cube like so;

alt text

Given the way my texture is set up I would expect that when I click near the corner I've marked "A", I would find a texture Coord of roughly (0, 0.6), B would be (0.33, 1), C (0.33, 0) and D (0.66, 0.4). However what I actually get is A = (0, 0), B = (1, 1), C = (0, 0), D = (1, 1). And the same is true of all the other sides too.

Does anyone know what I am doing wrong?

texture.png (1.1 kB)
cube.png (158.0 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
0
Best Answer

Answer by Guy-Corbett · Jul 22, 2018 at 01:23 PM

@Bunny83, you were completely right about making sure I was using the same mesh for rendering and colliding. I'm just posting the details as a separate answer so I can accept it, without making it look like the problem was just that I wasn't using a MeshCollider at all, as per your first suggestion.

In my case I had started with the default cube object the editor creates for you. I replaced the default collider with a meshcollider and gave it a reference to the cube mesh that was there on the object by default. That would have worked all fine, however I also had my own script on the object which, among other things, modified the mesh's UVs. It did that by calling;

 GetComponent<MeshFilter>().mesh.uv = myCustomUVs

The problem being that the call to ".mesh" instantiates and returns a new copy of the mesh that the meshfilter uses from then on, but obviously my Mesh Collider was still using the original mesh with the default uvs. Simply adding the following, later in my script, fixed it.

 GetComponent<MeshCollider>().sharedMesh = GetComponent<MeshFilter>().mesh

Thanks for the help!

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 Bunny83 · Jul 20, 2018 at 06:34 PM

Do you actually have a MeshCollider on your gameobject? The BoxCollider can not return any texture coordinates. Actually any of the primitive colliders (Box, Sphere, Capsule) will not return any texture coordinates. Only the MeshCollider can actually refer to the Mesh it uses

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 Guy-Corbett · Jul 22, 2018 at 07:31 AM 0
Share

Yup, a $$anonymous$$eshCollider is there. Like I said I am getting texture co-ordinates back. It's acting exactly like it's mean't to return the coordinates within one face, i.e. I don't just get (0, 0) or (1,1) - click the middle of any side and I get (0.5, 0.5). I tried without a $$anonymous$$eshCollider for comparison and I get (0,0) no matter where I click, as expected.

avatar image Bunny83 Guy-Corbett · Jul 22, 2018 at 10:17 AM 0
Share

Are you sure the $$anonymous$$eshCollider is using the same $$anonymous$$esh and that you actually used the first UV channel? You could try using my UVViewer to check the UV channels of your mesh.

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

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

Why does my Mesh Collider RayCastHit only return a texturecoord on Unity Remote? 0 Answers

Getting UV coordinate with GraphicRaycaster 2 Answers

Why in Unity3D RaycastHit. textureCoord always return 0,0 after building the project? 1 Answer

Sphercast hitinfo TextureCoord rounded? 1 Answer

CustomRenderTexture ignores "ComputeScreenPos" 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