- Home /
How to touch select 3D objects
New to Unity and Scripting... I would like a simple example of selecting a 3D object by touching the screen. I am either terrible at searching or there aren't many simple examples out there (probably the former).
I am guessing that you need to make a raycast to detect the object but I'm not sure how to do this.
Once the object is selected I would like to drag it, but that's a separate question.
Answer by gfr · Oct 24, 2011 at 06:45 PM
Yes, that's what raycasting is used for:
var ray : Ray = Camera.main.ScreenPointToRay(touch.position);
var hit : RaycastHit;
if (Physics.Raycast(ray, hit, 100)) {
selectedObject = hit.transform;
}
Once you have remembered the object you can also it's transforms position when it's dragged.
If you don't want to have all objects selectable, you will want to check the objects properties to see wether it's selectable or use layer masks to limit the raycasting.
Answer by DaveA · Oct 24, 2011 at 08:30 PM
Make sure each object (or sub object) has some sort of Collider on it. Then you can implement an OnMouseDown handler in a script which you attach to that (those) object(s).
Your answer
Follow this Question
Related Questions
Android touch 3d Object event 1 Answer
How do you Draw a Line Using your Finger's Position on Android 3 Answers
android touch 0 Answers
Using raycasts to move a game object toward touch point. 0 Answers
Raycast Touch Android not working JS 2 Answers