- Home /
simple TOUCH of a 3D object in iOS
Hi,
I tried to find out how to simply detect the touch of a 3D object with iOS. But I could not make it work on device.
Can someone please help me out:
Say there is a cube called "cube" and the user touches it on a iOS device.
How can catch this event??
thank you so much for your help!!!
Answer by Ryuuguu · Feb 27, 2011 at 04:14 AM
Putting together Jessy's answer in to a piece of code. The Update() and TapSelect() are put on any ONE Object in the scene. I put it ion my main game controller. The Selected goes on each object you to be able to select.
// Update is called once per frame void Update () { TapSelect(); }
void TapSelect() { foreach (Touch touch in Input.touches) { if (touch.phase == TouchPhase.Began) { Ray ray = Camera.main.ScreenPointToRay(touch.position); RaycastHit hit ; if (Physics.Raycast (ray, out hit)) { hit.transform.SendMessage("Selected"); } } } }
public void Selected(){
Debug.Log("Selected",this);
}
This is in c#. When the screen is tapped the TapSelect does a Raycast and if anything is hit it sends the Selected() message to the transform.
This is great, thanks. I am using it to send a "On$$anonymous$$ouseDown" message ins$$anonymous$$d, so it serves as a transparent drop in replacement.
Answer by Jessy · Feb 15, 2011 at 12:47 PM
Cast a ray from the position of a touch.
http://unity3d.com/support/documentation/ScriptReference/Collider.Raycast.html
That code uses ScreenPointToRay, which you should, but it also uses Input.mousePosition, which is not right on iOS. The thing you'll need to replace it with is a Touch.position. There are some examples in the documentation about how to code input for iOS:
http://unity3d.com/support/documentation/ScriptReference/Input.GetTouch.html
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
iPhone touch events slowdown fps?!? 2 Answers
Magnifying Glass Unity iOS 0 Answers
Touch not working 1 Answer