2013-06-14 4 views
1

작은 문제를 해결할 수있는 누군가가 있기를 바랍니다.마우스가 화면 밖에있을 때 움직임을 멈추는 방법

현재 입력 도구가 주 카메라에 연결되어있어 사용자가 마우스를 창 가장자리로 이동하여지도 주위를 화면 이동시킬 수 있지만 수정하려고 시도한 약간의 문제가 발생했습니다. 나 자신을 아무 소용이 없습니다.

마우스가 창 밖으로 나가면 여전히 패닝이 발생하여 디버깅 중이거나 다른 응용 프로그램을 사용할 때 불안감을 느낍니다. 그래서 누군가가 마우스가 게임 창 바깥에있을 때 움직임을 막을 수 있기를 바랍니다.

다음은 입력 관리자 코드입니다.

using UnityEngine; 
using System.Collections; 

public class InputManager : MonoBehaviour { 

    public Vector3 position = new Vector3(0,0, -10); 
    public int boundary = 50; 
    public int speed = 4; 

    private int screenBoundsWidth; 
    private int screenBoundsHeight; 

    // Use this for initialization 
    void Start() 
    { 
     screenBoundsWidth = Screen.width; 
     screenBoundsHeight = Screen.height; 

    } 

    // Update is called once per frame 
    void Update() 
    { 
     if (Input.mousePosition.x > screenBoundsWidth - boundary) { 
      position.x += speed * Time.deltaTime; 
     } 

     if (Input.mousePosition.x < 0 + boundary) { 
      position.x -= speed * Time.deltaTime; 
     } 

     if (Input.mousePosition.y > screenBoundsHeight - 10) { 
      position.y += speed * Time.deltaTime; 
     } 

     if (Input.mousePosition.y < 0 + boundary) { 
      position.y -= speed * Time.deltaTime; 
     } 

     Camera.mainCamera.transform.position = position; 
    } 
} 

감사합니다.

편집

나는 주위에 해키 작업과 함께 온,하지만 여전히 윈도우의 외부 주변의 특정 위치에서 발생하는 움직임을 발생합니다. 나는 누군가가 더 나은 해결책을 찾길 바란다.

if (Input.mousePosition.x < screenBoundsWidth && Input.mousePosition.y < screenBoundsHeight) { 
    if (Input.mousePosition.x > screenBoundsWidth - boundary) { 
     position.x += speed * Time.deltaTime; 
    } 
} 

if (Input.mousePosition.x > 0 && Input.mousePosition.y > 0) { 
    if (Input.mousePosition.x < 0 + boundary) { 
     position.x -= speed * Time.deltaTime; 
    } 
} 

if (Input.mousePosition.y < screenBoundsHeight && Input.mousePosition.x < screenBoundsWidth) { 
    if (Input.mousePosition.y > screenBoundsHeight - 22) { 
     position.y += speed * Time.deltaTime; 
    } 
} 

if (Input.mousePosition.y > 0 && Input.mousePosition.x > 0) { 
    if (Input.mousePosition.y < 0 + boundary) { 
     position.y -= speed * Time.deltaTime; 
    } 
} 
+0

이것은 WinForms 용입니까? – user959631

+0

불행하게도 Unity3D 용이지만 C#을 사용하고 있습니다. – Flickayy

답변

0

3 개 아이디어가 될 수 있습니다

같은과 같이 더 verbously 쓸 수
Rect screenRect = new Rect(0,0, Screen.width, Screen.height); 
if (!screenRect.Contains(Input.mousePosition)) 
    return; 

:

float mouseX = Input.MousePosition.x; 
float mouseY = Input.MousePosition.y; 
float screenX = Screen.width; 
float screenY = Screen.height; 

if (mouseX < 0 || mouseX > screenX || mouseY < 0 || mouseY > screenY) 
    return; 

// your Update body 

... 당신의 "ha cky "솔루션 (완전히 유효한 imho입니다).

다른 옵션은 각 화면 테두리에 4 개의 Rect 개체를 만든 다음 마우스가 해당 사각형 안에 있는지 확인하는 것입니다. 예 :

public float boundary = 50; 
public float speed = 4; 
private Rect bottomBorder; 
private Rect topBorder; 
private Transform cameraTransform; 

private void Start() 
{ 
    cameraTransform = Camera.mainCamera.transform 
    bottomBorder = new Rect(0, 0, Screen.width, boundary); 
    topBorder = new Rect(0, Screen.height - boundary, Screen.width, boundary); 
} 

private void Update() 
{ 
    if (topBorder.Contains(Input.mousePosition)) 
    { 
     position.y += speed * Time.deltaTime; 
    } 

    if (bottomBorder.Contains(Input.mousePosition)) 
    { 
     position.y -= speed * Time.deltaTime; 
    } 

    cameraTransform.position = position; 
} 

여기 까다로운 부분 Rect 좌표 Y 축 아래쪽 가리키고 Input.mousePosition는 Y 너무 bottomBorderRect 위에이어야 ... 가리키는 가지고 있는지, 그리고 topBorder 하단에 있어야한다. 왼쪽 및 오른쪽 테두리는 영향을받지 않습니다.

+0

그런 심층적 인 대답에 감사드립니다. 나는 당신이 어떻게 말했는지를 알기 위해 이걸 얻을 수 있는지 알게 될 것입니다.다시 고마워! – Flickayy

+0

개인적으로 나는 첫 번째 해결책을 찾아갔습니다. 고맙습니다! – Flickayy

0

Unity와 다양한 호스트 운영 체제가 상호 작용하기 때문에 마우스 커서를 제한적으로 제어 할 수 있습니다. (즉, OS가 마우스 유니티를 제어한다는 것은 단지 그것을 읽습니다.) 당신은 일부 옵션을 가지고 있다고 말합니다. Screen.lockCursor가 마음에 듭니다.

http://docs.unity3d.com/Documentation/ScriptReference/Screen-lockCursor.html

그것은 당신이 찾고있는 것을 정확히하지 않을 것이다 그러나 그것은 좋은 출발점을

+0

나는 이것을 조사해 보았고, 내가 만든 것을 달성하지 못하는 RTS 게임에 대해서, 나는이 게시물에 잘못된 제목을 부여했을지도 모른다. – Flickayy

0

Time.speed = 0;

원하는 내용은 무엇입니까?

관련 문제