3D Pickup
3D game actually see is 2D screen, we click on the screen, want to find which 3D object, we are actually in a 2-dimensional plane to do 3D pickup.
3D pickup In fact, when the player taps the screen, a ray is emitted from the camera on the display screen, and the first object that the ray hits is the object that the player has chosen.
1: The game needs users to touch/click 3D 3D objects inside the world, then you need to determine what the user clicked 3D in the object;
2:3d pickup principle: From the camera to the screen space of the touch point emit a ray, this ray first hit which 3D object will think which 3D object is selected by the user;
3: Code Writing
(1) emitting a ray: Ray Ray = Camera.main.ScreenPointToRay (touch.position);
(2) Detect bump into that object: Raycast hit; BOOL Physics.raycast (Ray, out hit);
(3) Hit.transform, obtain the object's transform component, name can obtain the name of the object being collided;
(4) Camera.main get current our main Camera
(5) If you want to pick up, you need to have a collider
3D Pickup Instance
1. Create a Unity Engineering catalog
2. Create a cube cube with box Collider ( other objects that want to participate in 3D pickup must have a collider component )
3. Create a script to mount the ray_test under the cube node
4. Open Ray_test
usingUnityengine;usingSystem.Collections; Public classRay_test:monobehaviour {//Use this for initialization voidStart () {}//Update is called once per frame voidUpdate () {if(Input.getmousebuttondown (0)) {//determine if the mouse is pressed//starting from the camera, to the screen touch point, emit a rayRay Ray =Camera.main.ScreenPointToRay (input.mouseposition); //which 3D object was hit.Raycasthit hit; if(Physics.raycast (Ray, outHit )) {Debug.Log (hit.transform.name);//print out the name of the node that collided } } }}
5. Click Cube in the game view and the console prints the cube's name cube
Attention:
Get the main camera method, main is the data member
Camera.main
About 3D Pickup in unity