Raycast Get Extention of the Object Hitted

Goodmorning developers
I was trying to create a script which helps me to get the extension of the object hitted.
I mean, I have a physics raycast, if this hits something, then get the nearest extension of the object, I mean to get the Vector3 of direction for the ray.

the Red Line is the physics raycast, and the blue is the extensione.
Thanks developers. Have a nice week end!

What is “extension”… In this context?

For extension I mean the length of the object, I don’t know how the explain to you.
The mesh vertex which is furthest to the max right or to the max left.
This may help the ai to avoid, use this point as waypoint.

Ok, I get you now, ill make something quickly and post it.

You are too gentle, Thank you so much.

Sorry, I could not make it work… I would have though something like this would have worked, but sadly not :frowning: but it may give some idea of it… it is only half the code the other would be comparing the distances from the array but unfortuantly I didn’t have time for that, but this can tell you how to do that(in c# i used US)

#pragma strict
var dist : float[];

function Update()
{
//[RAYCAST CODE START]
//all this should be in a raycast code
var mesh : Mesh = hit.GetComponent(MeshFilter).mesh;//make sure that the "hit" is the object it hit
var verts = mesh.vertices;
for(var i = 0; i > verts.length; i++)
{
var currentVert = transform.TransformPoint(mesh.vertices[i]);
dist[i] = Vector3.Distance(transform.position, currentVert);
print(dist[i]);
}
//compare the distances of the new dist array, then take the closest one to hit.
//[RAYCAST CODE END]
}

Anyway, that is kind of what you need to do, compare the location of the object to the vert point using TransformPoint.

Right, you gave me an perfect Idea, I think if we improve this code without distances, but with Transform.TransformPoint or Transform.InverseTransformPoint (I don’t know the differences, but Ionly know that only one of this works!). Then use Mathf.Max for all vector3s on X axis for right and Maths.Min for left.
After that compare this two left and right with Mathf.Abs, we can get the vertice which we need. Thanks I am going to write this in the code, After this I will take a look to the link.
Thank you so much, Have a nice week end!

I have implemented the code:

But this is the error:

At line number 2
I have tried to use

But same error.
How can I fix this?

Resolved the bug.
It was because I didn’t assigned it.
I am having the problem that the result of GetNearestExit (hit : RaycastHit) : Vector3 is always Vector3, I think that I have to modify something in the loop.
What may be?

If you’ve got a collider you can use collider.ClosestPointOnBounds.

Yes I got it, but it still anyway returning Vector3.zero