• 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 DissonanceMask · Jul 03, 2019 at 09:14 PM · screentoworldpointmouse positionmathf.clamp

Is it possible to Mathf.Clamp the position.x of Camera.main.screenToWorldPoint(Input.mousePosition)?

Im trying to make an Arkanoid-like game, using my mouse as the controller. How will I be able to apply Mathf.Clamp to my mouse if i'm using Camera.main.screenToWorldPoint(Input.mousePosition)?

so far i've made :

public class Paddle : MonoBehaviour { [SerializeField] float mousePosInUnits; [SerializeField] float minX = 1f; [SerializeField] float maxX = 15f; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { mousePosInUnits = Camera.main.ScreenToWorldPoint(Input.mousePosition).x; Vector3 paddlePos = Camera.main.ScreenToWorldPoint(Input.mousePosition); paddlePos.y = transform.position.y; paddlePos.z = transform.position.z; paddlePos.x = Mathf.Clamp(mousePosInUnits, minX, maxX); transform.position = paddlePos; } }

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Jul 04, 2019 at 12:32 AM

Yes, you can clamp the position, however the result is most likely not what you might expect. You're just clamping the position, you do not confine the mouse in that space. So the mouse can still move outside of your defined area but the position will be clamped. It's usually better to juse use a "virtual" mouse position and lock and hide the hardware mouse cursor. Now you can simply use the Mouse C axis which returns the current movement of the mouse in the x direction. Just scale it with your desired speed / sensitivity and add it to your position. That position can be clamped just fine.

Something like this:

 void Update()
 {
     if (Input.GetMouseButtonDown(0))
     {
         Cursor.lockState = CursorLockMode.Locked;
         Cursor.visible = false;
     }
     var p = transform.position;
     p.x += Input.GetAxis("Mouse X") * sensitivity;
     p.x = Mathf.Clamp(p.x, minX, maxX); 
     transform.position = p;
 }

Note that the Mouse X axis is a delta value that is already frame based. Do not multiply it with Time.deltaTime. When you do the movement would become framerate dependent.


The mouse will be locked when the user presses the mouse down for the first time. You may also want to add something like:

     if (Input.GetKeyDown(KeyCode.Escape))
     {
         Cursor.lockState = CursorLockMode.None;
         Cursor.visible = true;
     }

to unlock the mouse when the user presses ESC.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

109 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Making an object follow follow the mouse in 2D 1 Answer

ScreenToWorldPoint has some sort of offset 1 Answer

How do you use the ScreenToWorldPoint for receive the real mouse position ? 1 Answer

Is rendering to RenderTexture changing output of Camera.ScreenToWorldPoint()? 1 Answer

Spawning Enemies randomly within the screen 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