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");
}
}
}
}
}
}
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?
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.
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");
}
}
}
}
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.