我正嘗試使用Unity引擎從頭開始創建RTS遊戲。我從這個Unity RTS game tutorial開始,我正在設置相機。基本上我所做的是,我生成[x,0,z]
矢量運動。然後我需要將其應用於相機。TransformDirection在創建RTS攝像頭時行爲不如預期
如果相機沒有旋轉,它可以很好地工作 - 但它通常是旋轉的並且必須是可旋轉的。
根據教程and the docs,我可以使用Camera.main.transform.TransformDirection(MyVector)
將矢量轉換爲相機的視角。這是我做的:
//Get mouse position
float xpos = Input.mousePosition.x;
float ypos = Input.mousePosition.y;
//Create vector for movement
Vector3 movement = new Vector3(0, 0, 0);
//horizontal camera movement
if (xpos >= 0 && xpos < ResourceManager.ScrollWidth)
{
movement.x -= ResourceManager.ScrollSpeed;
}
else if (xpos <= Screen.width && xpos > Screen.width - ResourceManager.ScrollWidth)
{
movement.x += ResourceManager.ScrollSpeed;
}
//vertical camera movement
if (ypos >= 0 && ypos < ResourceManager.ScrollWidth)
{
movement.z -= ResourceManager.ScrollSpeed;
}
else if (ypos <= Screen.height && ypos > (Screen.height - ResourceManager.ScrollWidth))
{
movement.z += ResourceManager.ScrollSpeed;
}
//make sure movement is in the direction the camera is pointing
//but ignore the vertical tilt of the camera to get sensible scrolling
movement = Camera.main.transform.TransformDirection(movement);
//Up/down movement will be calculated diferently
movement.y = 0;
但是,如果我這樣做,垂直運動不爲inital相機輪流工作,當我旋轉的攝像頭,運動發生在陌生的速度。
如何正確應用相機上的運動矢量?
非常感謝你幫助我。最後一個微小的故障:如果我將相機*旋轉超過90°(所以它是「顛倒」),頂部/底部方向是顛倒的。我將通過將角度限制爲最大值來修復它。 – 2014-11-03 22:52:49