Question by
Chimpulous · Jun 03, 2019 at 11:11 PM ·
runtimegridsnapping
How do I account for rotation in a snap to function?
I am trying to create a snap to function that also allows for rigid body rotation and position changes. I need it to snap a block to a rounded point relative to the mouse. At the moment I am rounding the mouse position to an int and I have the master objects position in account, but when the master object rotates the sub object i'm snapping will change positions more the further out the mouse is. Here is the relevant code.
'''
public void SnapToGrid() {
Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 masterObjectPos = new Vector2(dataManagement.masterObject.transform.position.x, dataManagement.masterObject.transform.position.y);
Vector2 mouseToMasterObjectPos = masterObjectPos - mousePosition;
float mouseMasterObjectDistance = Vector2.Distance(masterObjectPos, mousePosition);
Vector2 camPos = new Vector2(Camera.main.transform.position.x, Camera.main.transform.position.y);
mousePosition -= masterObjectPos;
Debug.LogWarning("MOUSEPtomasterpos: " + mouseToMasterObjectPos);
Debug.LogWarning("mouseobject distance " + mouseMasterObjectDistance);
int xCount = Mathf.RoundToInt(mousePosition.x / gridSizeX);
int yCount = Mathf.RoundToInt(mousePosition.y / gridSizeY);
Vector2 result = new Vector2((float)xCount * gridSizeX, (float)yCount * gridSizeY);
result += masterObjectPos;
result += mouseToMasterObjectPos;
template.transform.position = result;
} '''
any help would be appreciated. Thanks!
Comment