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
0
Question by Carson365 · Jan 14, 2018 at 12:37 AM · inputscreenonmousedownleftright

Check where mouse is clicked

Hello! When you use Input.GetMouseButtonDown(0), what's a way where you could tell where it is pressed? Pretty much I want it to be like where if you click the left of the screen, it does a function, and if you press the right, another function. Please help me a little with coding, epically with raycasting since I am pretty new to this stuff. 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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by EDevJogos · Jan 14, 2018 at 12:44 AM

If that's all that you want no raycast is needed just check if Input.mousePosition.x is greater than Screen.width * 0.5f, if so your click was on the right side of the screen, otherwise left.

 private void Update()
 {
     if(Input.GetMouseButtonDown(0))
     {
         if(Input.mousePosition.x > Screen.width * 0.5f)
         {
             //Right side.
         }
         else
         {
             //Left side.
         }
     }
 }
Comment
Add comment · Show 5 · 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 Carson365 · Jan 14, 2018 at 12:46 AM 0
Share

Thanks for the quick response. I will try to do that, thanks.

avatar image Carson365 · Jan 14, 2018 at 12:52 AM 0
Share

So that solved my first problem. (Btw it worked perfectly!) So I am using Input.Get$$anonymous$$ouseButtonDown(0) as a "key". I need it to represent A/D key on the keyboard. I replaced it in the Input manager. This helps me move my character back and fourth, along with mobile. So far I have it where if you press the mouse button, the player moves a little then stops. It also does that if you hold it down. How can I make it where if you hold the the mouse button, the player will continue to move, like the WASD keys. Thanks.

avatar image Carson365 · Jan 14, 2018 at 01:03 AM 0
Share

Script:

     void FixedUpdate () {
         float moveHorizontal = Input.GetAxis ("Horizontal");
         print (moveHorizontal);
 
         Vector3 movementLeft = new Vector3 (1, 0, 0);
         Vector3 movementRight = new Vector3 (-1, 0, 0);
 
         if (Input.Get$$anonymous$$ouseButtonDown(0)) {
             if (Input.mousePosition.x < Screen.width * 0.5f) {
                 transform.Translate (movementLeft * moveHorizontal * speed * Time.deltaTime);
             }
             else {
                 transform.Translate (movementRight * moveHorizontal * speed * Time.deltaTime);
             }
         }
 
         if (transform.position.y > -0.5f) {
             transform.Translate (Vector3.forward * speedForce * Time.deltaTime);
         }
 
         else {
             return;
         }
     }
avatar image EDevJogos Carson365 · Jan 14, 2018 at 01:08 AM 0
Share

Change Input.Get$$anonymous$$ouseButtonDown(0) to Input.Get$$anonymous$$ouseButton(0)

avatar image Carson365 · Jan 14, 2018 at 01:10 AM 0
Share

Got it to work. Ended up I didn't need the if (Input.Get$$anonymous$$ouseButtonDown(0)) in the first place because it was already in the input manager. Thanks a lot for your help!

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

87 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

Related Questions

Samsung 42" Touch Screen: Touch response issues 1 Answer

Fluid motion not working on input 1 Answer

How do I make my sprite go left or right when it hits a trigger? 0 Answers

How to get a rigidbody to move left and right? 1 Answer

Walking forward 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