- Home /
Detect mouse movement when cursor is on edge of screen?
I am trying to make a simple mouse look script where you move the camera with the mouse, like in an fps. I know my script is probably not the most efficient way I am new to unity and I'm just doing this to practice. The problem with my script is that once the cursor hits the edge of my screen I can now longer look in that edge's direction because I can't move my mouse anymore in that direction. Does anyone have ideas on how a can prevent this? Here is my script:
public float speed;
public Vector3 mouseDelta = Vector3.zero;
private Vector3 lastMousePosition = Vector3.zero;
void Update ()
{
mouseDelta = Input.mousePosition - lastMousePosition;
lastMousePosition = Input.mousePosition;
if (Input.GetAxis("Mouse Y") < 0)
{
transform.Rotate(new Vector3(speed * Mathf.Abs(mouseDelta.y) * Time.deltaTime, 0, 0));
}
if (Input.GetAxis("Mouse Y") > 0)
{
transform.Rotate(new Vector3(-speed * Mathf.Abs(mouseDelta.y) * Time.deltaTime, 0, 0));
}
if (Input.GetAxis("Mouse X") < 0)
{
transform.Rotate(new Vector3(0, -speed * Mathf.Abs(mouseDelta.x) * Time.deltaTime, 0), Space.World);
}
if (Input.GetAxis("Mouse X") > 0)
{
transform.Rotate(new Vector3(0, speed * Mathf.Abs(mouseDelta.x) * Time.deltaTime, 0), Space.World);
}
}
Answer by Skyfall106_Gaming · Mar 13, 2017 at 09:50 AM
I had a similar problem in my game and I got a script that moves when you put your curser near the screen, rotates with q and e and moves with wasd. Im not sure if this helps but I think if you get bits of it out it should work the way you want it to :)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WASDMove : MonoBehaviour {
public float ScrollSpeed = 15;
public float ScrollEdge = 0.1f;
public float PanSpeed = 10;
public Vector2 zoomRange = new Vector2( -10, 100 );
public float CurrentZoom = 0;
public float ZoomZpeed = 1;
public float ZoomRotation = 1;
public Vector2 zoomAngleRange = new Vector2( 20, 70 );
public float rotateSpeed = 10;
private Vector3 initialPosition;
private Vector3 initialRotation;
void Start () {
initialPosition = transform.position;
initialRotation = transform.eulerAngles;
}
void Update () {
// panning
if ( Input.GetMouseButton( 0 ) ) {
transform.Translate(Vector3.right * Time.deltaTime * PanSpeed * (Input.mousePosition.x - Screen.width * 0.5f) / (Screen.width * 0.5f), Space.World);
transform.Translate(Vector3.forward * Time.deltaTime * PanSpeed * (Input.mousePosition.y - Screen.height * 0.5f) / (Screen.height * 0.5f), Space.World);
}
else {
if ( Input.GetKey("d") ) {
transform.Translate(Vector3.right * Time.deltaTime * PanSpeed, Space.Self );
}
else if ( Input.GetKey("a") ) {
transform.Translate(Vector3.right * Time.deltaTime * -PanSpeed, Space.Self );
}
if ( Input.GetKey("w") || Input.mousePosition.y >= Screen.height * (1 - ScrollEdge) ) {
transform.Translate(Vector3.forward * Time.deltaTime * PanSpeed, Space.Self );
}
else if ( Input.GetKey("s") || Input.mousePosition.y <= Screen.height * ScrollEdge ) {
transform.Translate(Vector3.forward * Time.deltaTime * -PanSpeed, Space.Self );
}
if ( Input.GetKey("q") || Input.mousePosition.x <= Screen.width * ScrollEdge ) {
transform.Rotate(Vector3.up * Time.deltaTime * -rotateSpeed, Space.World);
}
else if ( Input.GetKey("e") || Input.mousePosition.x >= Screen.width * (1 - ScrollEdge) ) {
transform.Rotate(Vector3.up * Time.deltaTime * rotateSpeed, Space.World);
}
}
// zoom in/out
CurrentZoom -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * 1000 * ZoomZpeed;
CurrentZoom = Mathf.Clamp( CurrentZoom, zoomRange.x, zoomRange.y );
transform.position = new Vector3( transform.position.x, transform.position.y - (transform.position.y - (initialPosition.y + CurrentZoom)) * 0.1f, transform.position.z );
float x = transform.eulerAngles.x - (transform.eulerAngles.x - (initialRotation.x + CurrentZoom * ZoomRotation)) * 0.1f;
x = Mathf.Clamp( x, zoomAngleRange.x, zoomAngleRange.y );
transform.eulerAngles = new Vector3( x, transform.eulerAngles.y, transform.eulerAngles.z );
}
}
Answer by Taylor-Howell · Jul 14, 2020 at 01:50 PM
Here's a hacky way to do it:
public void Start()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
Basically, with the first line, (Cursor.lockState), you lock the cursor to the center of the screen. Then you can turn off the cursor by setting Cursor.visible to false.
If you need to use the Cursor again, hit Escape and you should be good to go.
I wouldn't say that's hacky, I think it's the intended way of doing it. Not locking the cursor and reading it's screen position is prone to problems if the game is windowed, and requires a lot more bug-prone code.