5
\$\begingroup\$

I'm currently working on a grid for snapping objects side by side. My current script is working fine, but only for game objects which haven't been rotated.

Here is an example video of the problem:

https://vid.me/jN6D

The cube at the right has a rotation of Vector3.zero, the left one has been rotated on the Y axis.

As you can see snapping works perfectly for the right one, but at 0:15 you can see that the snapping doesn't work well for the rotated cube, that's because I've currently no idea how I can add the rotation in my current script.

Here is a snippet of the most important part of code I'm currently using to calculate the grid:

Vector3 pivotToPoint = hit.point - nearestGameObject.transform.position;

float positionX = nearestGameObject.transform.position.x + 
                  Mathf.Round(pivotToPoint.x / nearestGameObject.transform.renderer.bounds.size.x) * 
                  nearestGameObject.transform.renderer.bounds.size.x;

float positionZ = nearestGameObject.transform.position.z + 
                  Mathf.Round(pivotToPoint.z / nearestGameObject.transform.renderer.bounds.size.z) * 
                  nearestGameObject.transform.renderer.bounds.size.z;

Vector3 origin = new Vector3(positionX, 0, positionZ) + Vector3.up * 100;

if (Physics.Raycast(origin, Vector3.down, out hit, Mathf.Infinity, layerMask)) {
    previewGameObject.transform.position = new Vector3(positionX, hit.point.y, positionZ);

    previewGameObject.transform.rotation = nearestGameObject.transform.rotation;
}

Maybe some of you guys have an idea how to solve this, it's driving me crazy.

p.s. Here is some guy with a similar problem, but I'm very lost when in comes to adding the solution into my code.

\$\endgroup\$
1
\$\begingroup\$

It's been a while since I've worked in Unity, but I believe the problem (or at least part of it) you're having may be that you're using renderer.bounds which gives you the axis aligned bounding box of the object in question. This would mean that when you rotate the bounding box is not rotating, but is in fact growing to fit the rotated object

You can test this out by outputting the bounds values you're getting out of the non rotated and the rotated object, this should also help you find the exact points where things are going wrong if you output the pieces going into the positionX and positionZ floats individually so you can compare between the working case and the non working one

Since the rotation seems to be accurate the main thing you need to focus on is the position, and with that you should be able to simply use half the size of each along the given axis summed together. since you should have the rotation of the box you are snapping to you should be able to apply it to the standard x and z directions to get the direction to offset in, which can then be multiplied by each correct distance to get the proper axis offsets.

On a side note, I highly recommend using things like Debug.DrawLine and Gizmos.DrawWireCube to help visualize what is going on in situations like these as they tend to make it easier to assess what is going on with various 3d direction things as you adjust rotation/position/...etc


Note: I'm assuming Unity because the Raycast and the renderer.bounds, if I'm wrong on that most of this should still apply, you'll just need to test whether bounds is axis aligned and figure out what the proper debug draw methods are for your situation

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.