문제

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top