About 3D Pickup in unity

Source: Internet
Author: User
Tags emit

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.