- Home /
Get RaycastHit Local Coords (C#)
First of all, if you use JS and not C#, I know them both very well and can convert it, as I used JS for around a year before switching to C#.
Now, I know this is probably a really stupid question, but I need to convert RaycastHit.point to a local space ON an object. I don't exactly know how to explain this, so I make a simple example picture.
So, if anyone could please tell me how to have this returned as a Vector2, or Vector3 if needed, then thank you!
I imagine it's something to do with its current position and its scale, but I'm unsure of exactly what to do, this is probably something very very easy.
Thanks in advance!
Answer by Bunny83 · Oct 04, 2016 at 12:54 AM
Well, to transform a world position into a local position you can simply use "InverseTransformPoint" of your target object. So the resulting point is relative to the target objects space. This is actually the reverse method of TransformPoint which transforms a localspace position into worldspace.
However, keep in mind that localspace might be something different then what you think. What you've drawn looks like texture / UV space. However if the origin of that plate is at that 0, 0 point it should work if the object has a unscaled size of "1".
That is why I said that it will probably have something to do with the scale, so you could do whatever divided by the scale.
I will give this a test and reply back in about an hour when I can, thank you!
TransformPoint as well as InverseTransformPoint will take into account the objects position, rotation and scale. That's what a "space" is made of.
For example Unity's default cube mesh has a size of 1.0 in each direction but the pivot is in the center so the corners are at +-0.5 in each direction. If you scale the cube up so it's 5x5x5, the local points of the corners are still at +- 0.5
We don't know how "large" your object is in local space. If it's 3x0.1x4 you would need to divide the x position by the x-size (3 in this case), the y position by the y-size (0.1 here) and the z position by the z-size (4) to get a range of "0 - 1"
If your pivot is not at one of the corners you need to offset the position. So if the pivot is in the center (like the default cube) you would simply add 0.5 to each axis after you divided by the size.
Note: size and scale are two completely different things. Size can mean the actual size of the object either scaled or unscaled. The scale is just a multiplication factor.
If you don't know the size of the object you can use $$anonymous$$esh.bounds of your object's mesh. It gives you the local space bounding box of the mesh.
It's generally easier to answer a question when we know what this all is about (so what's the actual goal). Abstract questions can only receive abstract answers.
Thank you!
Transform.InverseTransformPoint worked perfectly!
The origin is at the center, but that's actually better.
The goal was a locking system for structure placing, but I needed to check what corner you are looking at for pillars, what side for walls and other floors, etc.
The chunk I used to test it, if anyone needs it.
if(lookingAt.transform.gameObject.name == "Floor_type{STRUCTURE}"){
Vector2 point = lookingAt.transform.InverseTransformPoint(lookingAt.point);
print(point);
}