- Home /
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.
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");
}
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.
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.
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).
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.
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.
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.
@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.
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.
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.
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
Your answer
Follow this Question
Related Questions
RaycastHit Texturecoord doesn't work with separate colliders 0 Answers
RaycastHit.textureCoord ranges from (0,0) to (1,1) for each face. 2 Answers
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