- Home /
Question by
CustomChaos · Apr 10, 2016 at 09:41 AM ·
c#2dinput
Divide screen in half for two buttons
Is there anyway to make the below code work for a mouse click instead of a touch? I have tried all sorts of things however i keep getting errors in my code.
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch (0);
if (touch.position.x < Screen.width/2)
{
//Move Player Left
}
else if (touch.position.x > Screen.width/2)
{
//Move Player Right
}
}
else
//Do nothing for now
}
Any help would be much appreciated. Still a newbie :)
Comment
Best Answer
Answer by jni97 · Apr 10, 2016 at 10:11 AM
I changed your code to mouse clicks
if(Input.GetMouseButtonDown(0))
{
if (Input.mousePosition.x < Screen.width/2)
{
//Move Player Left
}
else if (Input.mousePosition.x > Screen.width/2)
{
//Move Player Right
}
}
else {
//Do nothing for now
}
If you want to detect right mouse button clicked instead of the left one, just change GetMouseButtonDown(0) to GetMouseButtonDown(1).