因此,我有一种将网格位置转换为全局空间的方法:
public Vector3 GridToGlobal(IVector3 gridPos)
{
Vector3 globalPos = new Vector3(gridPos.x, gridPos.y, gridPos.z);
globalPos *= Scale;
globalPos = Container.transform.rotation * globalPos;
return globalPos;
}
现在,当我想进行反向操作时,如何将向量旋转到网格空间中?
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));
}
有任何想法吗?
解决方法:
您可以使用Quaternion.Inverse()函数.这使反向旋转.所以应该扭转.
您可以通过以下方式使用它:
transform.rotation = Quaternion.Inverse(target.rotation);
来源:http://docs.unity3d.com/Documentation/ScriptReference/Quaternion.Inverse.html