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");
}
}
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.
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.
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.
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?
It works, you just need to replace Debug.Log with a code that controls the movement.
Your answer