Scale object problem

I’ve been working with the code for 2 weeks, so don’t swear, I just don’t know a lot more.
Made a script that identifies another object by tag, and if it finds it, you can attach another to it. And everything works correctly as long as Scale = 1.
But I need the object to be Scale = 0.5. And when I reduce it to 0.5, the object is no longer dense, but with a gap of just 0.5.
pos + = hit.normal;
where pos is Vector3 pos = hit.collider.transform.position;
and hit is RaycastHit hit; that is, a variable with a position.
Well, normal is normal. It seems to me that this is because of the normal, but I took a simple cube from the unit itself for the experiment and scaled it.
When creating, it turns out that what is in the picture. How do I fix this?
https://ibb.co/G7Zk13Q

To densely pack the cubes, you need to know how big those cubes are.

As you said, default cube has size of 1 unit, and incidentally this is the size of the normal. So if you do “pos += hit.collider.normal”, the cube will be placed EXACTLY one way away from the hit point ,regardless of how large the cube actually is.

So, you’ll have to store cube size somewhere and do “pos += hit.collider.normal * cubeSize” (in this case the size will have to be a scalar)

Alternatively, the cube prevfab (you ARE using prefabs, yes?) likely has a box collider attached to it, which you can grab with GetComponentInChildren:

And then query box bounds using corresponding property.

Another possibility is using Renderer.bounds, but that one is imprecise and axis aligned.

2 Likes

pos += hit.collider.normal * cubeSize"

Oh yeah it really worked, thanks a lot.