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 xorap · Jun 14, 2018 at 07:29 PM · raycasthittexturecoord

RaycastHit.textureCoord not working on my non-Convex collider.

I am using a script to construct a procedural mesh, then send raycasts from all vertices along the left edge (0,y) to locate the points where my intersecting object meets the mesh.

 for (int i = 0; i < vertices.Length; i++) { 
             Vector3 _i = new Vector3 (0, vertices [i].y);
         RaycastHit hit; 
         if (Physics.Raycast (vertices [i], Vector3.right, out hit, ySize + 1, layermask)) {
                 Debug.Log ("hit");

Then from those points, I do another raycast, with a z offset, going back to the direction of the hit.points. I tested with gizmos, to verify if that I could hit my own mesh with a raycast. But have since removed them. When I convert the second phase of raycasts to this:

 RaycastHit dosHit;
         if (!Physics.Raycast (new Vector3 (hits.point.x, hits.point.y, -10), hits.point, out dosHit, 15, layermask)) {
             Debug.Log ("no hit");
         }
         Renderer rend = dosHit.transform.GetComponent<Renderer>();
         MeshCollider meshCollider = dosHit.collider as MeshCollider;
 
         if (rend == null || rend.sharedMaterial == null || rend.sharedMaterial.mainTexture == null || meshCollider == null) {
             Debug.Log ("no renderer, material, texture or collider");
         }
         Vector2 shoreline = dosHit.textureCoord;

I get "no hit". I've set all of my references in the inspector, layers are correct, my generated collider is non convex and I even transferred my phase2 raycast to another script, even still unity logs "no hit" in the play test.

I'll include these hastebin links to either script and gameobject aswell.

Main Script

RayRelay

Comment
Add comment · Show 5
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 xorap · Jun 14, 2018 at 07:45 PM 0
Share

After adding this script to my ray relay im finding that I cant even collide with my generated mesh...

 public Transform pointer;
     public Transform target;
     private void OnDrawGizmos(){
         Gizmos.color = Color.white;
         RaycastHit hit;
         if (Physics.Raycast (pointer.position, target.position, out hit, 15, layermask)) {
             Gizmos.DrawSphere (hit.point, .5f);
         } else {
             Debug.Log ("its not really there");
         }
avatar image xorap · Jun 15, 2018 at 04:13 PM 0
Share

alt text

There is definitely a problem with my collider, because the gizmo sphere draws whenever I include the 3D Sphere into my layermask. but not when I leave it to only the procedural mesh. alt text

2018-06-15-12-10-51.png (356.0 kB)
2018-06-15-12-11-12.png (351.2 kB)
avatar image xorap · Jun 15, 2018 at 04:25 PM 0
Share

alt text It seems, when I delete the mesh from my generated mesh collider, within the inspector, and then immediately undo the action (ctrl + z) the gizmo draws a sphere, but also a ray is drawn at 0,0.

2018-06-15-12-21-27.png (368.7 kB)
avatar image xorap xorap · Jun 15, 2018 at 04:31 PM 0
Share

Assigning the mesh collider after recalculating my normals seems to work, but its really slow. Also, the Vector2 for the texutrecoord/texturecoord2 is always (0,0).

avatar image xorap xorap · Jun 15, 2018 at 06:05 PM 0
Share

alt text

It's strange, but I am returning Vector2 texture coordinates, but when I multiply them by the texture dimensions and convert them to integers, they become 0. in the case of the screenshot, the texture is 32x32.

monodevelop-2018-06-15-14-03-41.png (128.2 kB)

2 Replies

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

Answer by Bunny83 · Jun 15, 2018 at 12:12 AM

The Raycast method does not take two positions as input but one position and one direction vector. You seem to pass a target position which makes no sense. If you want to specify a start and end point you should use a Linecast instead.

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 xorap · Jun 15, 2018 at 02:43 AM 0
Share

The raycast works fine. I just named my variables pointer and target. They just represent Vector3 coordinates, the ray fires from the pointer's position in the direction of the target.

avatar image EvilWarren · Jun 15, 2018 at 10:49 AM 1
Share

@Bunny83 is correct. Your 2nd raycast might be syntactically correct but it is not semantically correct. Look at the method signature for Raycast:


public static bool Raycast(Vector3 origin, Vector3 direction, out RaycastHit hitInfo, float maxDistance = $$anonymous$$athf.Infinity, int layer$$anonymous$$ask = DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);


On your Raycast call you have new Vector3 (hits.point.x, hits.point.y, -10) as the position and hits.point as the direction. As an example, plug in the value of hits.point as being a Vector3 with values (0,0,0). This would mean you have a position of (0,0,-10) (this is O$$anonymous$$) and a direction of (0,0,0) (this is not O$$anonymous$$). Based on your description of desired behaviour, your direction should actually be (hits.point - new Vector3 (hits.point.x, hits.point.y, -10)).normalized . Again plugging in a value of (0,0,0) for hits.point , this will give you a position of (0,0,-10) (this is O$$anonymous$$) and a direction of (0,0,1) (also O$$anonymous$$) pointing back to hits.point .

You should mark Bunny’s answer correct.

avatar image xorap EvilWarren · Jun 15, 2018 at 06:29 PM 0
Share

alt text

 private void OnDrawGizmos ()
     {
         if (vertices == null) {
             return;
         }
         for (int i = 0; i < vertices.Length; i++) { 
             Vector3 _i = new Vector3 (0, vertices [i].y);
             Gizmos.DrawSphere (_i, 0.1f);
             RaycastHit hit; 
             if(Physics.Raycast (vertices [i], Vector3.right, out hit, ySize + 1, layermask)){
                 Gizmos.color = Color.white;
                 Gizmos.DrawSphere (hit.point, 0.1f);
                 //Debug.Log (hit.point);
             }
             RaycastHit doshit;
             if(Physics.Raycast(new Vector3(hit.point.x, hit.point.y, -10), (hit.point - new Vector3(hit.point.x, hit.point.y, -10)).normalized, out doshit, 11)){ 
                 Gizmos.color = Color.white;
                 Gizmos.DrawRay(new Vector3(hit.point.x, hit.point.y, -10), (doshit.point - new Vector3(hit.point.x, hit.point.y, -10)).normalized);
                 Gizmos.DrawSphere (doshit.point, 0.1f);
             }
         }
     }

I've updated my RayRelay, and added this gizmo code to the main script. That's just because it's been normalized. Without it, it looks like this. alt text

Still, seeking an answer, because I'm trying to return the texture coordinates. I'm getting an error, from the rayrelay not finding the renderer of my mesh.

unity-2018-06-15-11-57-14.png (318.5 kB)
2018-06-15-11-59-50.png (341.5 kB)
avatar image
0

Answer by xorap · Jun 15, 2018 at 07:14 PM

Thanks to Bunny83 and EvilWarren, I fixed my raycast. It turns out that I was shadowing my shoreline class variable. I was able to fix it from there and spawn a whole new set of problems, but this question has been answered. Thank you

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

86 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

Related Questions

Get pixel colour on a ray hit 1 Answer

RaycastHit.textureCoord is broken? 2 Answers

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

How to get Texture Name from textureCoord or RaycastHit in a Mesh with multiple materials? 0 Answers

Sphercast hitinfo TextureCoord rounded? 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