- Home /
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!
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.
}
}
}
Thanks for the quick response. I will try to do that, thanks.
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.
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;
}
}
Change Input.Get$$anonymous$$ouseButtonDown(0)
to Input.Get$$anonymous$$ouseButton(0)
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!