Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 ExpiredIndexCard · Mar 27, 2013 at 05:51 AM · mouserotatescreenmoveedge

How can I move the camera when the mouse reaches the edge of the screen?

Hey just tell me what I need to do. I need some guidance ahhaha sorry im not the best programmer. Thanks

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

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Chronos-L · Mar 27, 2013 at 06:47 AM

Try this function.

 Vector2 MouseScreenEdge( int margin ) {
    //Margin is calculated in px from the edge of the screen
 
    Vector2 half = new Vector2( Screen.width/2, Screen.height/2 );
 
    //If mouse is dead center, (x,y) would be (0,0)
    float x = Input.mousePosition.x - half.x;
    float y = Input.mousePosition.y - half.y;   
    
    //If x is not within the edge margin, then x is 0;
    //In another word, not close to the edge
    if( Mathf.abs(x) > half.x-margin ) {
       x += (half.x-margin) * ( x < 0 )? 1 : -1;
    }
    else {
       x = 0f;
    }
    
    if( Mathf.abs(y) > half.y-margin ) {
       y += (half.y-margin) * ( y < 0 )? 1 : -1;
    }
    else {
       y = 0f;
    }
    
    return new Vector2( x, y );
 }

You define a margin of the edge of the screen, then this function will return a measurement on how close is the mouse to the edge.

For example, margin = 20:

 Vector2 mouseEdge = MouseScreenEdge( 20 );

If mouse.Edge.x is 0 (you need to use Mathf.Approximately()), then you have not touched the left or right side of the mouse. If mouse.Edge.x is about 5, then your mouse is 15px away from the right side of the screen. If mouse.Edge.x is about -20, then your mouse is on the leftmost side of your screen. The same goes for the y-axis.

From the Vector2 returned by this function, you can do something like this:

 void Update() {
    Vector2 mouseEdge = MouseScreenEdge( 20 );
 
    if( !( Mathf.Approximately( mouseEdge.x, 0f) ) {
       //Move your camera depending on the sign of mouse.Edge.x
 
       if( mouseEdge.x < 0 ) {
          //Move Left
       }
       else {
          //Move right
       }
    }
 }

Code is not tested, but I checked the calculation twice; so I am quite confident that this will work.

Comment
Add comment · Show 4 · 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 Chronos-L · Mar 27, 2013 at 06:55 AM 1
Share

Another way to use this script is that if you use a Vector2 as margin, and then you insert the half screen-size a margin. The vector2 returned by the function will indicate which quadrant the mouse is at.

avatar image ExpiredIndexCard · Mar 27, 2013 at 11:01 PM 1
Share

THAN$$anonymous$$S! It seemed that you put the most effort in trying to explain this so I gave you best answer. Thanks!

avatar image Chronos-L · Mar 28, 2013 at 12:15 AM 0
Share

You can modify the function to use Vector2 as parameters if you need different margin on the left-right and top-bottom.

I am a very lazy fella, so I always try to make my function/code as reusable as possible; so I can re-adapt it to another game without hassle.

I am glad that I could help.

avatar image Preyer · Oct 14, 2013 at 12:44 PM 0
Share

This is a great solution as the value you get also tells you how FAR from the edge the pointer actually is. It makes it easy to move the camera faster the closer to the edge it is. Thanks!

The code almost works, just 4 small fixes. $$anonymous$$athf.abs is actually $$anonymous$$athf.Abs (lines 12 and 19) and both ( x < 0 )? 1 : -1 (line 13) and ( y < 0 )? 1 : -1 (line 20) need to go in parenthesis.

avatar image
1

Answer by nuverian · Mar 27, 2013 at 06:19 AM

You can translate the camera's transform on it's .x axis by Input.GetAxis("Mouse X") (which return a value from -1 to +1. And do this only if the Input.mousePosition.x is at the screen edge Screen.width So:

 var camTransform:Transform;
 var speed:float;
 
 if (Input.mousePosition.x == Screen.width){
 
    camTransform.position.x += Input.GetAxis("Mouse X") * Time.deltaTime * speed;
 }
 
 if (Input.mousePosition.x == 0){
 
    camTransform.position.x -= Input.GetAxis("Mouse X") * Time.deltaTime * speed;
 }
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 Seth-Bergman · Mar 27, 2013 at 06:26 AM

Well, not much point in explaining then

 var sensitivity = 10;
 
 function Update(){
 
 if(Input.mousePosition.x < sensitivity)
 transform.Rotate(-Vector3.up * Time.deltaTime * Mathf.Abs(sensitivity -Input.mousePosition.x);
 
 
 else if(Input.mousePosition.x > Screen.width - sensitivity)
 transform.Rotate(Vector3.up * Time.deltaTime * Mathf.Abs(Screen.width -Input.mousePosition.x);
 
 }

add this javascript to your camera (untested..)

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

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

13 People are following this question.

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

Related Questions

Move the camera when the mouse reaches the edges of the screen 1 Answer

Detect cursor on edge of screen 1 Answer

How do I rotate an object on one axis to face android touch? 0 Answers

Click-to-move problems, mouse cursor moving 3 Answers

Multiple rotate with mouse 2 Answers


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