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.
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.
asked Jan 17 '10 at 04:48 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.)
answered Jan 28 '11 at 06:01 PM