- Home /
Unity3d Get Cube Pressed On Mobile
I am trying to get my game to sense when the player touches a cube on-screen, but so far I cannot get it to work. I am using:
if (Input.touchCount > 0 && Input.touchCount < 2)
{
}
But it picks up input from anywhere on the screen, and for as long as I touch the screen. How can I get this to work?
Comment
Please stop asking questions here without following up on the answers and comments.
I see on your profile that you keep making new questions and leaving old questions behind when people are trying to help you.
This leaves a mess behind for people who google the same question and find half-finished issues from you.
Best Answer
Answer by Wisearn · Dec 20, 2014 at 11:14 PM
What you most likely need to do is something like:
if (Input.touchCount > 0 && Input.touchCount < 2)
{
Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
RaycastHit hit;
if (Physics.Raycast(ray, out hit)) {
//more conditions here based on hit.collider.gameObject.name for example
}
}