Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
0
Question by AVividLight · Mar 10, 2011 at 05:59 PM · mouserestrict

Restrict the mouse posisition

Hey guys,

I was wondering how I would restrict how far the mouse can go on the screen... I was thinking about using mathf.clamp, but I don't really know how... Anyway, if you guys can help, that would be great!


//Written By/For Gibson Bethke var crosshairTexture : Texture2D;

function Update () { Screen.showCursor = false; }

function OnGUI () { var mousePos = Event.current.mousePosition; GUI.DrawTexture( Rect( mousePos.x - (crosshairTexture.width/2), mousePos.y - (crosshairTexture.height/2), crosshairTexture.width, crosshairTexture.height), crosshairTexture); }


Just incase I wasn't clear, I want the mouse to stop before it hits the sides of the screen, much like this (but with the mouse):

//Written By/For Gibson Bethke var normalMoveSpeed : float = 3.125; var topMoveSpeed : float = 6.25; var currentSpeed : float = 3.125;

function Update () { var down = Vector3(0, -1, 0);

     if(Input.GetButton("Jump")) {
         currentSpeed = topMoveSpeed;
     }else{
         currentSpeed = normalMoveSpeed;
     }
         if(Input.GetButton("W")) {
             transform.Translate(Vector3.up * currentSpeed);
     }
         if(Input.GetButton("A")) {
             transform.Translate(Vector3.left * currentSpeed);
     }
         if(Input.GetButton("S")) {
             transform.Translate(Vector3.down * currentSpeed);
     }
         if(Input.GetButton("D")) {
             transform.Translate(Vector3.right * currentSpeed);
     }
      var xMove : float = Input.GetAxis("Horizontal") * Time.deltaTime * 20;
         transform.Translate(Vector3(xMove,0,0));
         transform.position.x = Mathf.Clamp(transform.position.x, 300, 1700); 

     var zMove : float = Input.GetAxis("Vertical") * Time.deltaTime * 20;
         transform.Translate(Vector3(zMove,0,0));
         transform.position.z = Mathf.Clamp(transform.position.z, 300, 1700);

}

-Thanks so much for your help!

P.s. Both the scripts work, the top one is what I have so far

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
2
Best Answer

Answer by Statement · Mar 10, 2011 at 06:04 PM

Set minX, maxX, minY, maxY to the values you want it to clamp between.

mousePos.x = Mathf.Clamp(mousePos.x, minX, maxX);
mousePos.y = Mathf.Clamp(mousePos.y, minY, maxY);


function OnGUI () { var mousePos = Event.current.mousePosition;

 mousePos.x = Mathf.Clamp(mousePos.x, minX, maxX);
 mousePos.y = Mathf.Clamp(mousePos.y, minY, maxY);

 GUI.DrawTexture( Rect( mousePos.x - (crosshairTexture.width/2),
                        mousePos.y - (crosshairTexture.height/2),
                        crosshairTexture.width,
                        crosshairTexture.height), crosshairTexture);

}

Comment
Add comment · Show 7 · 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 AVividLight · Mar 10, 2011 at 06:26 PM 0
Share

It says: $$anonymous$$ identifier: 'mousePos'.

avatar image Statement · Mar 10, 2011 at 06:33 PM 1
Share

Well, you need to have a mousePos. Just add it where you have one.

avatar image AVividLight · Mar 10, 2011 at 07:05 PM 0
Share

There we go, it worked! Thanks

avatar image AVividLight · Mar 10, 2011 at 07:18 PM 0
Share

Do you think you could answer one more question? How would I restrict the mouse's movement speed?

avatar image Statement · Mar 10, 2011 at 10:03 PM 0
Share

I guess you could sample the position and store it to the next frame. Then use Vector3.Clamp$$anonymous$$agnitude on the delta. http://unity3d.com/support/documentation/ScriptReference/Vector3.Clamp$$anonymous$$agnitude.html

Show more comments
avatar image
0

Answer by Rakshaan · May 24, 2021 at 09:57 AM

easy way I use to restrict cursor to a distance from edges of screen: use the newMousePos as your cursor value.

   public float mousePosX_min;
   public float mousePosX_max;
   public float mousePosY_min;
   public float mousePosY_max;
 
 void Update()
 {
       float mousePosX = Mathf.Clamp(Input.mousePosition.x,mousePosX_min, mousePosX_max); 
       float mousePosY = Mathf.Clamp(Input.mousePosition.y,mousePosY_min, mousePosY_max); 
       newMousePos = new Vector2(mousePosX,mousePosY);
 }
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 Jack_Veritac · Mar 17 at 07:10 PM 0
Share

float mousePosX = Mathf.Clamp(Input.mousePosition.x, minClamp, maxClamp); float mousePosY = Mathf.Clamp(Input.mousePosition.y, minClamp, maxClamp); Vector2 newMousePos = new Vector2(mousePosX, mousePosY); mousePosition = newMousePos;

nothing happened

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

restrict mouselook X movement script does not work 4 Answers

Game runs on one screen, cursor wanders onto other screens? 0 Answers

Cursor flickers while changing 0 Answers

Mouse Glitching? 0 Answers

Rotate Object with with mouse 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