- Home /
 
Unity touch input
using UnityEngine; using System.Collections;
public class Character_movement : MonoBehaviour { public GameObject Player;
 void Update()
 {
     foreach (Touch t in Input.touches)
     {
         Ray ray = GetComponent<Camera>().ScreenPointToRay(t.position);
         RaycastHit hit;
         Debug.Log("s");
         if (Physics.Raycast(ray, out hit)) 
         {
             GameObject thing = hit.transform.gameObject;
             if (Touch.phase == TouchPhase.Began) 
             {
             }
         }
     }
 }
 
               Here is what I got so far. I'm trying to make a small ball go to a pad when you tap it but how do I do that?
 
Answer by FlippyPls · Jun 13, 2017 at 12:21 AM
Don't sue me if I'm wrong, but I think you use Input.GetKey(KeyCode.Mouse0) for touch as you would for a computer mouse.
So raycast it and if(hit.transform.name == "pad") Ball.transform.position = hit.transform.position
Hope I could help!
This is what I got so far but still not working
{ public GameObject Player; RaycastHit hit;
 void Update()
 {
     foreach (Touch t in Input.touches)
     {
         Ray ray = GetComponent<Camera>().ScreenPointToRay(t.position);
         if(hit.transform.tag == "Pad")
         {
             Debug.Log ("BOIII!");
             Player.transform.position = hit.transform.position;
         }
     }
 }
 
                  }
Here is the error: Object reference not set to an instance of an object
I know what this error means but I can't see the error. The error takes me to the if statement the tag is set to Pad.
Your answer
 
             Follow this Question
Related Questions
Android input only works with remote 1 Answer
Mouse and Oculus Touch Controller Inputs 0 Answers
EventSystem.current.IsPointerOverGameObject(t.fingerId) - Not working on Android builds - Unity 2 Answers
How do I render Oculus Touch controllers in-game in Unity? 1 Answer
IOS object touch, collider 1 Answer