So I have a method that transforms a grid position into global space:
public Vector3 GridToGlobal(IVector3 gridPos)
{
Vector3 globalPos = new Vector3(gridPos.x, gridPos.y, gridPos.z);
globalPos *= Scale;
globalPos = Container.transform.rotation * globalPos;
return globalPos;
}
Now when I want to do the reverse, how would I go about rotating the vector into the grid space ?
public IVector3 GlobalToGrid(Vector3 globalPos)
{
globalPos -= Container.transform.position;
globalPos = Container.transform.rotation * globalPos; //this will obviously rotate in the wrong direction
globalPos /= Scale;
return new IVector3((int)Mathf.Round(globalPos.x), (int)Mathf.Round(globalPos.y), (int)Mathf.Round(globalPos.z));
}
Any ideas?
You can use the Quaternion.Inverse() function. This gives back the opposite rotation. So it should reverse. You can use it this way:
transform.rotation = Quaternion.Inverse(target.rotation);
Source: http://docs.unity3d.com/Documentation/ScriptReference/Quaternion.Inverse.html