- Home /
Increment grid by rotation
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:
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.
I'm willing to pay the one who could fix that problem for me.
http://forum.unity3d.com/threads/very-quick-job-for-experienced-programmer.331697/
The question isn't really clear. Do you mean you want to be able to rotate your grid? o.O That sounds a bit strange. If you have two different rotations objects places in one rotation can't possible fit to another in a different rotation.
Do you want just a temporary local grid that rotates with the player? $$anonymous$$eep in $$anonymous$$d as soon as you rotate just a fraction of a degree the grids won't match anymore.
Or do you want a fix grid. If it's a fix grid in worldspace how do you want put rotation in there?
You should be way more specific what kind of behaviour you expect. If you rotate the grid there's no way to match the new grid cells with the cells of the unrotated grid in any way that makes sense.
$$anonymous$$aybe you should add a bit more details for what purpose you want to use that grid snapping?
edit
Oh, another question: Do you want to snap a rotated object to an unrotated object as well? How do you actually rotate your object? The whole picture of your problem has a lot dark spots.
For me only the snapping is important. It should work no matter how the target/nearest object's y axis is rotated.
So to answer your question, the grid should always use the nearestObject position/rotation as starting point, so that the rotation of the new object (to spawn) aligns with it as soon the mouse cursor is in range (as shown in my first video).
Answer by JoeStrout · Jun 08, 2015 at 04:49 PM
You need to do the snapping in the local coordinate system of the nearestGameObject, instead of in world coordinates. Use nearstGameObject.transform.InverseTransformPoint to convert from world to local, then round to the nearest grid position, then use .TransformPoint to convert this back to world.
I'm not sure if I understand your suggestion right. So I should use nearstGameObject.transform.InverseTransformPoint with hit.point?
e.g. nearstGameObject.transform.InverseTransformPoint(hit.point);
Then calculating the snapping with the result of that method and calculate it back with nearstGameObject.transform.TransformPoint(snapcalculatedvector3)?
Yes, that sounds about right. You've already got basic snapping working -- you just need to do it in the nearestGameObject's local coordinate system, ins$$anonymous$$d of in the world coordinate system.
Thanks, that worked. Please send me a pm regarding the payment. :)
I'm happy it worked! No need for payment, but if you really want to thank me, you can buy a copy of High Frontier. :)
Answer by FortisVenaliter · May 22, 2015 at 04:25 PM
Maybe something like this? Untested and may need some modifications, but hopefully it will get you in the right direction.
Vector3 offset = new Vector3(
Mathf.Round(pivotToPoint.x / nearestGameObject.transform.renderer.bounds.size.x) * nearestGameObject.transform.renderer.bounds.size.x,
0,
Mathf.Round(pivotToPoint.z / nearestGameObject.transform.renderer.bounds.size.z) * nearestGameObject.transform.renderer.bounds.size.z);
offset = nearestGameObject.transform.rotation * offset;
float positionX = nearestGameObject.transform.position.x + offset.x;
float positionZ = nearestGameObject.transform.position.z + offset.z;
Thanks for your answer. It seems like that's the right direction, here is how it looks like with the rotated cube:
Your answer
![](https://koobas.hobune.stream/wayback/20220613200151im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Snapping an object to another 0 Answers
How To Sync Animation To Music or Bpm ? 2 Answers
Stop a line renderer from rotating when new segments are added? 1 Answer
Snap to grid not working for me 1 Answer
Grid Increment by Rotation 2 Answers