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
0
Question by KingNeo Gaming · Aug 10, 2014 at 08:31 PM · c#clickholdleft

Unity 4 C-Sharp Do Something when I hold left click on a specific object?

Hi, I'm relatively new to Unity as well as C-Sharp, and im trying to make a game for mobile in which there are 3 controls on screen. An arrow facing right, one facing left, and one facing up.

What I wish to achieve is when the player holds their finger on the right arrow button (IE GetMouseButton(0)) The in-game player will move to the right at a certain rate.

I have been searching around trying to find answers, and most come to the idea of Raycasting, which I have never used before. Basically, if someone could tell me how to do something only when the user holds the mouse button down on ONE SPECIFIC OBJECT, something will happen. Also, the buttons never move, they will stay on screen at all times.

Just for some more information, this is a 2D based game, and I have a Box Collider 2D on each of my 3 buttons, if that makes a difference. 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
0

Answer by radiKal07 · Aug 10, 2014 at 09:47 PM

Well you didn't specified what type are the arrows. If they are sprites and have a collider atached to them you can do something like this:

 Vector3 tmpPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);   
                 
 if (collider2D == Physics2D.OverlapPoint(tmpPos) && Input.GetMouseButton(0) == true)
 {
 //code here                
 }

If you say they will never change position I suggest to use GUITexture instead because they will look the same on every resolution and stay in the same position whatever the resolution or aspect ratio. For GUITexture you can use this:

 if (guiTexture.HitTest(Input.mousePosition) && Input.GetMouseButton(0) == true)
 
 {
 //code here
 }

You said you want this to be for mobile so for touch controls you do like this:

 if (Input.touchCount > 0)
 for (int i = 0; i < Input.touchCount; i++)
 {
 Touch touch = Input.GetTouch(i);
 
 //here you copy-paste the above code and instead of "Input.mousePosition" put "touch.position"
 
 }


Comment
Add comment · Show 3 · 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 KingNeo Gaming · Aug 11, 2014 at 01:18 AM 0
Share

I actually managed to figure it out, I used a void On$$anonymous$$ouseDrag() for each of the individual control buttons in their own C# scripts in order to maneuver my character. Thank you for the quick reply though, I will definitely use that info to help build games simpler in the future.

avatar image KingNeo Gaming · Aug 11, 2014 at 01:58 AM 0
Share

One Question Though, could i use that code for multiple different buttons and do something different with each one? Also, would I put that code under void Update()? And one more item, how could I utilize multi-touch to be able to press one button and jump while holding another and moving forwards?

avatar image radiKal07 · Aug 11, 2014 at 06:07 AM 0
Share

Yes but be carefull to detect properly which button was pressed. And yes this goes in Update() because it is input check and must happen every frame. Even if you use GUITexture you put that code in Update() not OnGUI().

For multi-touch it's like this. Input.GetTouch(i) is an array of all touches so if you go from i=0 to i=Input.touchCount you will check all touches. If you would use something like Input.GetTouch(0) you only check one touch. So for every button you atach a script to it and check Input.GetTouch(i).position and if it hit the button you do an action.

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

A node in a childnode? 1 Answer

Script Reference using C # 1 Answer

Camera Orbit on Left Click problem 0 Answers

Error CS0016 in Unity? 0 Answers

Make Health script lose health when certain animation is played? 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