Unity3d Ray Detection

Source: Internet
Author: User

Question: In the League of Legends, your mouse click on the map where, your hero will go to where you click, then, with u3d how to achieve this small function???

Solution: do radiographic testing. First you have to prepare two things (1): Camera (This is the starting point of the Ray)

(2): "Collision Information" (information used to record the collision point generated by the ray and the object)

Create an empty project in U3d and add a C # script: Raycasttest. Add the two things we just said: "Camera", "Collision info", and initialize them both (the camera will be initialized later)

Hang the script on the camera (Main camera) and use Maincamera to assign the public variable camera

A plane object and a cube object are then created (the object is set to zoom (0.2,0.2,0.2) for reference at the origin of the world coordinate system), and both coordinates are set to (0,0,0)

Continue to write the code in the update:

Now run the project, you can see my test results: At this point I put the mouse on the top of the cube, because cube in the world coordinate system far point, so see the left side of the printed results x,z are 0, and y is greater than 0 (because the cube's top face in the world coordinate system Y axis)

In this, we can get the position information of the point in the world coordinate system that the mouse clicked, and the mouse will see different results in the 1,2,3,4 quadrant I marked.

Outside: Can add a method to facilitate testing, so that only a mouse click, will print a message.

Unity3d Ray Detection

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.