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 /
  • Help Room /
avatar image
3
Question by xxluky · Jan 01, 2015 at 08:22 PM · touchscreen

Touch left or right side of screen

Hey,

I am just a begginer and I need to help with a script when touching left or right side of the screen by mouse. The code bellow is just example, does not work.

     void Update () {
 
         if (Input.GetMouseButtonDown (0) > Screen.width / 2) {
                 Debug.Log ("RightTouch");
         }
     
         if (Input.GetMouseButtonDown (0) < Screen.width / 2) {
                 Debug.Log ("LeftTouch");
         }
     }
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 ChocolateMooseland · Mar 03, 2018 at 08:11 AM

I wrote a script similar to that when trying to port one of my games to Android. While the clicks registered properly, the functions that I wrote to execute after the click didn't work properly.

While there's probably a better solution out there, what I just did was create 2 buttons that spanned the entire left and right of the screen, deleted their text objects, changed the images to a completely transparent image (UIMask will work) then wrote this script to get the buttons to work.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ScriptName : MonoBehaviour
 {
     public void LeftMove()
     {
         // Write function here
     }
     public void RightMove()
     {
         // Write function here
     }
 }

As I said, there's probably a better solution but this has worked for me so far.

Comment
Add comment · Show 2 · 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 xxluky · Mar 04, 2018 at 06:01 PM 0
Share

Hey, I am not working on the game anymore. I do not know, how I finally figured it out, but as time goes by I am not a begginer now and I know that your suggested answer will definetelly work. It might be very usefull for other developers. I appreciate it. Thanks.

avatar image Wolverine-X-man · Oct 05, 2020 at 10:05 AM 0
Share

Thanks $$anonymous$$an

Thanks For Your Idea...

avatar image
2

Answer by Graham-Dunnett · Jan 01, 2015 at 08:24 PM

Have you read the docs? If you have then you'll know that the functions you are using return bool and not a pixel position. Maybe you want to use Input.mousePosition.

Comment
Add comment · Show 2 · 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 xxluky · Jan 02, 2015 at 08:55 AM 1
Share

Hi, ok, I found this script:

     void Update () {
         if (Input.touchCount > 0)
         {
             var touch = Input.GetTouch(0);
             if (touch.position.x < Screen.width/2)
             {
                 Debug.Log ("Left click");
             }
             else if (touch.position.x > Screen.width/2)
             {
                 Debug.Log ("Right click");
             }
         }
     }

What shoud I do whith this to get it working? Even when I attache it to the camera or player, it does not work... I need to attache the script to the player. Can you please help?

avatar image Dadusak xxluky · May 19, 2018 at 06:00 AM 0
Share

It works, you just need to replace Debug.Log with a code that controls the movement.

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

6 People are following this question.

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

Related Questions

Why is my IOS tap function is not working? 0 Answers

Need some help with Touchscreen Input. 1 Answer

How can I block Physical Raycasts from going through UI Elements? 0 Answers

Touch Controls inaccurate after screen sleeping 0 Answers

air hockey problem with independent controls 0 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