- Home /
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!
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"
}
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.
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?
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
![](https://koobas.hobune.stream/wayback/20220613160305im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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