Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Need help converting simple mouse input to touch input

Discussion in 'Scripting' started by HorizonHD, Mar 25, 2016.

  1. HorizonHD

    HorizonHD

    Joined:
    Jan 27, 2015
    Posts:
    11
    I want to convert getKeyDown(KeyCode.Mouse0) to a simple touch input that picks up when you touch the screen once, no multi touch fancyness, Ive tried lots of tutorials but nothing seems to work the way I want it to
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    if (Input.GetTouch(0).phase == TouchMode.Began) {
    //Do stuff
    }

    Usually unity docs is the best tutorial you can get, just check it out
     
  3. HorizonHD

    HorizonHD

    Joined:
    Jan 27, 2015
    Posts:
    11
    I tried that but it says touchmode doesnt exist in this current context, ive tried touchphase but nothing happens when I touch the screen, and I get the error array out of bounds
     
    Last edited: Mar 25, 2016
  4. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Yeah, I typed the wrong one. You need to do a null check before attempting this, because getTouch returns a list of touches, so before that: if (GetTouch.Length > 0). Can you post the script you're using? I swear I used it and it works
     
  5. HorizonHD

    HorizonHD

    Joined:
    Jan 27, 2015
    Posts:
    11
    Heres what I have, nothing happens when I tap the object this scripts attached to, it should delete the object.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class Meteor : MonoBehaviour {
    6.     public static int intScore = 0;
    7.     public int Health;
    8.  
    9.  
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.  
    14.     }
    15.    
    16.    
    17.     // Update is called once per frame
    18.     void Update () {
    19.         CheckInput();
    20.        
    21.     }
    22.  
    23.  
    24.  
    25.     void CheckInput()
    26.     {
    27.         if(Input.touchCount > 0)
    28.         {
    29.             if (Input.GetTouch(0).phase == TouchPhase.Began)
    30.             {
    31.                 RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
    32.  
    33.                 if (hit.collider != null)
    34.                 {
    35.                     if (hit.collider.gameObject == gameObject)
    36.                     {
    37.                         intScore = intScore + 1;
    38.                         Health = Health - 11;
    39.                         CheckHealth();
    40.                     }
    41.                 }
    42.             }
    43.         }
    44.     }
    45.  
    46.  
    47.  
    48.  
    49.     void CheckHealth()
    50.     {
    51.         if(Health < 0)
    52.         {
    53.             Destroy(gameObject);
    54.         }
    55.     }
    56. }
     
  6. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Put the physics.raycast in an if and use out hit and camera.ScreenPointToRay instead like this:
    if (Physics.Raycast2D(yourcamera.ScreenPointToRay(Input.mousePosition), out hit) {
    if (hit.collider.gameObject........)​
    }
    leave out the null check
    What you did wrong:
    Vector2.zero as a direction? That won't work
    Why the null check if you can put it in an if?

    You should really watch a tutorial about this (there's sure a unity tutorial, watch that)