- 最后登录
- 2018-6-29
- 注册时间
- 2011-7-1
- 阅读权限
- 20
- 积分
- 359

- 纳金币
- 335582
- 精华
- 0
|
The Update function in the editor script in this example project has code for scrolling. Including the edge of the screen method, arrow keys, and middle mouse button method; pretty much all the RTS games I've played have at least the first two. Not including the public variables for scrolling speed and so on, the relevant code is:
var mPosX = Input.mousePosition.x;
var mPosY = Input.mousePosition.y;
// Do camera movement by mouse position
if (mPosX < scrollArea) {myTransform.Translate(Vector3.right * -scrollSpeed * Time.deltaTime);}
if (mPosX >= Screen.width-scrollArea) {myTransform.Translate(Vector3.right * scrollSpeed * Time.deltaTime);}
if (mPosY < scrollArea) {myTransform.Translate(Vector3.up * -scrollSpeed * Time.deltaTime);}
if (mPosY >= Screen.height-scrollArea) {myTransform.Translate(Vector3.up * scrollSpeed * Time.deltaTime);}
// Do camera movement by keyboard
myTransform.Translate(Vector3(Input.GetAxis("EditorHorizontal") * scrollSpeed * Time.deltaTime,Input.GetAxis("EditorVertical") * scrollSpeed * Time.deltaTime, 0) );
// Do camera movement by holding down option or middle mouse button and then moving mouse
if ( (Input.GetKey("left alt") || Input.GetKey("right alt")) || Input.GetMouseButton(2) ) {
myTransform.Translate(-Vector3(Input.GetAxis("Mouse X")*dragSpeed, Input.GetAxis("Mouse Y")*dragSpeed, 0) );
}
|
|