- Home /
Touch problems
I would like the following script to move an object as you move your finger across the mobile device. This script gives me no compiler errors, but it also doesn't register... like at all. The debug isn't even popping up in the console.
using UnityEngine;
using System.Collections;
public class PlatformSpawning : MonoBehaviour {
Ray ray;
RaycastHit hit;
public GameObject platform;
public GameObject region;
void Update () {
if (Input.touchCount >= 1)
{
foreach (Touch touch in Input.touches)
{
Ray ray = Camera.main.ScreenPointToRay(touch.position);
RaycastHit hit;
if(Physics.Raycast (ray, out hit, 100)){
if (hit.transform.name == "region" || hit.transform.name == "Platform")
{
platform.transform.position = hit.point;
Debug.Log ("Touched");
}
}
}
}
}
}
Answer by G4merSylver · Dec 15, 2015 at 02:50 AM
I am not exactly sure, but I think the reason there is no response might be because you arent exactly giving it an touch input? For the movementtracking of the mouse you would need to use input.getaxis. Touch input is for touch devices... I think? Also are you using the correct way of getting your touches by input.gettouch?
Answer by phil_me_up · Dec 14, 2015 at 07:57 PM
Are you running this on a PC / Mac? If so then the Input.touchCount will always be 0 as mouse input is handled separately. You'll need to expand your code to check the Input.mousexxx data.
Can you elaborate a bit more? I think I understand what you're saying, but I'm not sure.
On a computer, there's generally no such thing as a "touch" event. You would need separate handling for such inputs as Input.mousePosition and Input.Get$$anonymous$$ouseButtonDown. Otherwise, while "touch" input is not invalid on a computer, it will most likely not be recognized and called.
Answer by OncaLupe · Dec 15, 2015 at 02:11 AM
Your raycast is only 100 units, are you sure that's far enough to reach objects in the game? Do the objects have Colliders on them and named exactly as the checks are looking for? I'd add some more Debug.Logs, one for each loop/if block to see where it's stopping.
Also, you create variables 'ray' and 'hit' at the start of the class, then new ones in the Update(). Only one of these sets are needed, though aren't hurting anything.
(Note: I don't have a device to test on, but it looks good to me and no compile errors as you mentioned.)
using UnityEngine;
using System.Collections;
public class PlatformSpawning : MonoBehaviour
{
public GameObject platform;
public GameObject region;
void Update () {
if (Input.touchCount >= 1)
{
Debug.Log("Touching start");
foreach (Touch touch in Input.touches)
{
Debug.Log("Checking a touch");
Ray ray = Camera.main.ScreenPointToRay(touch.position);
RaycastHit hit;
if(Physics.Raycast (ray, out hit, 100))
{
Debug.Log("Raycast hit : " + hit.transform.name);
if (hit.transform.name == "region" || hit.transform.name == "Platform")
{
platform.transform.position = hit.point;
Debug.Log ("Touched");
}
}else
Debug.Log("Nothing hit");
}
}
}
}
I can tell you right now that none of the debugs are registering at all. Including the nothing hit one.
Answer by hexagonius · Dec 15, 2015 at 07:21 PM
If you do not even receive the "Touching start", there's just no input, or the script is attached to nothing. if it is though, use Unity remote on a connected touch device (cable) and be sure to see the game view on it, then try.
Your answer
Follow this Question
Related Questions
How can one rotate an object instantly? 2 Answers
Card Game like yu-gi-oh 1 Answer
First Person Controller - Problems with footstep audio being played too fast. 0 Answers
Iterate through area descibed by objects 1 Answer
MonoType Behavior 0 Answers