- Home /
Help with touch input for iOS please!
Well, I have been working in a system for touch input for my project for weeks now. I've tried with several different codes but none of them work properly. Here are some of them:
var i : int;
var hit : RaycastHit;
function Update ()
{
for(i = 0; i< Input.touchCount; i++)
{
var ray : Ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).position);
if(Physics.Raycast(ray, hit))
{
if(collider.bounds.Contains(hit.transform.localPosition))
{
if(Input.GetTouch(i).phase == TouchPhase.Began)
{
hit.transform.gameObject.SendMessage("OnTouchDown");
}
if(Input.GetTouch(i).phase == TouchPhase.Ended)
{
hit.transform.gameObject.SendMessage("OnTouchUp");
}
}
}
}
}
The problem with this one is that it doesn't do anything when tapping objects that are inside other objects (children objects). Aside from that it works good.
Next:
var i : int;
var hit : RaycastHit;
function Update ()
{
for(i = 0; i< Input.touchCount; i++)
{
if(Input.GetTouch(i).phase == TouchPhase.Began)
{
var ray = Camera.mainCamera.ScreenPointToRay(Input.GetTouch(i).position);
if(Physics.Raycast(ray, hit))
{
hit.transform.gameObject.SendMessage("OnTouchDown");
}
}
if(Input.GetTouch(i).phase == TouchPhase.Ended)
{
hit.transform.gameObject.SendMessage("OnTouchUp");
}
}
}
This code also works, but I think that for some reason it shoots a ton of rays on a single touch, the reason I believe this is because when I tap a button on the Main Menu of my game, it automatically taps the button that appears after selecting the first one. Another reason is that while playing, in theory if you tap a block it gives you one point and then the block disappears. When using this code, if you tap a block it gives you 9 points before disappearing.
Note: I am using this code as the "brain" for all the codes of the buttons (`class ScriptName extends TouchButtonScript`). I am using a 3DText with a box collider as my buttons.
Does anybody knows what I'm doing wrong? Thanks in advance.
Answer by Coderdood · Jun 05, 2013 at 06:47 PM
Problem 1:
The problem with this one is that it doesn't do anything when tapping objects that are inside other objects (children objects).
GameObject.SendMessage = "Calls the method named methodName on every MonoBehaviour in this game object."
Use GameObject.BroadCastMessage = "Calls the method named methodName on every MonoBehaviour in this game object or any of its children."
Note that you will get an error message if there was no receiver found unless you use SendMessage.DontRequireReceiver. I've missed that bit of documentation in the past.
I suggest you use the second script instead anyway.
Problem 2:
This code also works, but I think that for some reason it shoots a ton of rays on a single touch, the reason I believe this is because when I tap a button on the Main Menu of my game, it automatically taps the button that appears after selecting the first one.
The problem is this script should be attached to a only one object in the scene. By using this as the base (class ScriptName extends TouchButtonScript) every time you click a raycast for every object extending TouchButtonScript is send.
Create a single "TouchManager" or some such object and attach this script to that. I actually recommend this version over the first.
[4]: http://answers.unity3d.com/questions/202037/touchphase-firing-too-much-ios.html
Thank you, I used the second solution (for the second script). I added the TouchButtonScript to the $$anonymous$$ain Camera and erased the (class ... .. .. ) from all the scripts, it works great!
Your answer
Follow this Question
Related Questions
Detect a tap versus standing touch on iPhone? 3 Answers
Touch input reporting 0 delta for first several Updates on iOS, 2 Answers
How to replace input axis keyboard button with touch button? 3 Answers
Drag and Drop button on Mobile (Messenger style) 0 Answers
UI button touch input problem when UI is SetActive during game play 1 Answer