I have to convert my pong game from keyboard usage to touch based mechanics. However I am completely stuck trying to figure out Unity's touch mechanics. I have searched through the internet and everyone somehow keeps resolving the issue in ways that are still failing for me. Here is, from everything I have gathered, what I believe should work. This is in my update method so it is constantly checking this.
Touch t;
void Update() {
if (Input.touchCount > 0) {
print("touch seen");
t = Input.GetTouch(0);
}
}
The if statement condition is never met no matter how many times I touch the screen. I am using a lenovo laptop that has a touch screen, maybe it is reading my touches as clicks? I just can't seem to figure it out. If I take away the if statement I get an ArrayIndexOutOfBounds Exception. Any help is extremely appreciated!
Update It is registering as a mouse click, and I can't just allow this because I need multiple touch inputs.
You can use Input.touchSupported
to check if touch is supported on your computer. If it returns true
, then read from touch with Input.GetTouch(0)
. If it returns false
, then use Input.GetMouseButtonDown
to read from the mouse instead. Touch won't work if your computer does not support touch screen . Below is complete code on how to do this:
void Update()
{
if (Input.touchSupported)
{
Debug.Log("TOUCH IS SUPPORTED");
if ((Input.touchCount > 0) && (Input.GetTouch(0).phase == TouchPhase.Began))
{
print("touched screen");
}
}
else
{
Debug.Log("TOUCH IS NOT SUPPORTED");
if (Input.GetMouseButtonDown(0))
{
print("clicked screen");
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.