Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by BearForceFun · Mar 13, 2017 at 06:55 AM · c#mousescreenmouselook

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);
         }
     }



Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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 );
     }
 }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tuinal · Jul 14, 2020 at 02:17 PM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Fix Gimbal Lock Mouse Look? 0 Answers

Why does my mouselook sensitivity change depending on resolution? (C#) 1 Answer

How to disable MouseLook in ingame pause. 1 Answer

A projectile problem... (Mouse click using Ray/Plane) C# 0 Answers

Multiple Cars not working 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges