- Home /
Simple (?) touch-command on iPhone
Ive done the fps-tutorial a while back so I have some idea of how scripts work, but when it comes to the iphone-specific touchcommands Im a bit stumped.
Im simply trying to find a way to interact with the environment by clicking on various objects in the scene, and the first step in that process has been to try and figure out how this code-snippet retrieved from the docs works and how to implement it. Its supposed to just trigger a particle once the screen is touched, but I need it to do so only when the target object (lets say a cube) is touched.
var particle : GameObject;
function Update () {
for (var touch : iPhoneTouch in iPhoneInput.touches) {
if (touch.phase == iPhoneTouchPhase.Began) {
// Construct a ray from the current touch coordinates
var ray = Camera.main.ScreenPointToRay (touch.position);
if (Physics.Raycast (ray)) {
// Create a particle if hit
Instantiate (particle, transform.position, transform.rotation);
}
}
}
}
This is how I think the script works: In every frame a iPhoneTouch-object (touch) is created, which checks if the touch is a beginning touch. If so a ray is created from the camera to the touched position, and finally the particle-effect is triggered.
Is this correct, or have I misunderstood the function?
And if it is correct, how do I make it check if the ray has hit an object with a response?
And finally, where do I put this script? Should it be attached to each object that should respond to it (which would mean a lot of scripts running), or should it be attached to the player and have it check for each possible object the ray could hit with an if-else-structure?
Also, if you have a better codesnippet that incorporates not just one aspect that would be most welcome since codesnippets tend to be very illustrative and helpful in understanding concepts.
Thanks, Daniel
Answer by Bampf · Jan 17, 2010 at 08:11 PM
Physics.Raycast returns True if the ray hits any collider in the scene. If the object you are testing has a collider on it, and there are no others in the scene, then you should be good to go.
Otherwise, you can use layers to group colliders into those you are testing and those you want to skip. (Layer mask is an optional parameter to Physics.Raycast.) Or you can simply check the hitInfo structure that Raycast can give you to see what was hit (see docs, it depends which version of Raycast you call.)
The array of touches is good particularly if you want to respond to multi-touch, but if you are only interested in single touches you can simply use Input.GetButtonDown("Select"), which detects mouse clicks in Unity, and touches in Unity iPhone. (The argument to GetButtonDown is the name of a virtual button. You can define your own but "Select" is one that exists by default, and it maps to Mouse Button 1 in Unity, and a single touch in Unity iPhone. Here is a link to an overview of the Input manager.)
Is this the correct usage?
1) I create an object and name it "Door1".
2) I attach a script to the player's update-function as follows:
function Update () { if (Input.GetButtonDown ("Door1")) { // code for switching scene to "Scene_1", for instance } }
So every time I make a touch in the game it will check wether the "Door1"-object has been pressed and execute the script.
Putting it in the Update function is correct, but Input.GetButton("Select") is just detecting that a mouse click or touch has occurred. (Note: "Select" is the name of a virtual button; you can define new ones but that's a different topic. I've added documentation links about that to my answer above.) After teh click or touch has happened you still need the raycast to deter$$anonymous$$e if it hit anything.
Also, rather than handling the logic for doing stuff when something is hit by the player within the player class, have a look at Send$$anonymous$$essage http://unity3d.com/support/documentation/ScriptReference/GameObject.Send$$anonymous$$essage.html and make the thing that was hit responsible for deter$$anonymous$$ing what happens.
Answer by cregox · Jan 28, 2011 at 06:01 PM
this:
// OnTouchDown.cs // Allows "OnMouseDown()" events to work on the iPhone. // Attach to the main camera.
using UnityEngine; using System.Collections; using System.Collections.Generic;
public class OnTouchDown : MonoBehaviour { void Update () { // Code for OnMouseDown in the iPhone. Unquote to test. RaycastHit hit = new RaycastHit(); for (int i = 0; i < Input.touchCount; ++i) { if (Input.GetTouch(i).phase.Equals(TouchPhase.Began)) { // Construct a ray from the current touch coordinates Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).position); if (Physics.Raycast(ray, out hit)) { hit.transform.gameObject.SendMessage("OnMouseDown"); } } } } }
I find the OnTouchDown.cs camera script adds a 1 second input delay on an iOS device, any idea why this might be happening?
@lion-gv sorry, I have no idea. but I get no delay on my end, so I'd suggest you trying an empty project, or even an empty scene without any other scene being loaded just to be sure, and try to add things little by little so you can diagnose where the issue might be. this code is not processor intensive so it shouldn't be bringing any issues on its own.
Your answer
![](https://koobas.hobune.stream/wayback/20220611235825im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
iPhone touch events slowdown fps?!? 2 Answers
OnCollisionEnter-issue 1 Answer
Working with Touch screens... 1 Answer
Ray error with touches.position 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers