Unity3d Independent game Development Diary (ii): placing buildings

Source: Internet
Author: User

In the sandbox game, can be free to build is very important features, such as the popular world of "My World", with a block can build a large-scale world. There are even extreme people who say that without the freedom to build, it is not a real sandbox game. Indeed, a great part of the charm of a sandbox game is the freedom to build a world of games. Look at yourself brick by brick build a castle the world will be very fulfilling.

Today's hands are mostly a world of battles and ostentation. Whether it is the legend of the crazy bully drag cool, or repeatedly see, the elimination of music and other friends ranked, is the vanity of consumers. In fact, the game is the Nineth art, to rise to the angle of art. In the game, the player needs an emotional catharsis and sustenance and experience.

Say so much, or return to the point. How do I make a building stand in my own independent game, "troubled: Homeland"?

In implementing this function, the first thing I think about is not the code. Instead, consider how other games can be used to achieve such a function, and there are places to learn from. I first thought of the "Warcraft" in the building of the display, you can freely drag and drop. Then think of the "Clash of Clans" (hereinafter referred to as the COC) in the construction of the way in the COC, the building is placed in a lattice, on a plane.

In my game "troubled: Homeland", I do not want to let the player brick by brick building, but also want to let the player put the whole building on the ground. I feel like building blocks that can save both player time and construction fun. With this approach in mind, I'm going to consider how the building blocks are stored: I need to store the coordinate values (x, Y, z) in the three-dimensional space of a single module, and I need to store the number of rotation angles of the model for the model selection.

Then, I also need to consider how to do this with the mouse. My idea is that when you click on an item in your backpack, a model is generated dynamically and then moved with the mouse over the ground. Then press the "E" key to place the mouse position on the ground (e key placement, I learned "Orc must Die" obtained). And what about the rotation of the components? What I'm thinking about is that when you scroll the mouse wheel (that is, the middle button), it rotates around the y axis. Of course, if one step closer, you can rotate around three axes freely. For specific operation, see:

Architectural Display Animation

Use your mouse to place a campfire

Through the above operation, we can see that the implementation of the code is very good, perfect to meet our needs. However, after figuring out the above operating principles, we also need to use our brains to conceive, how to implement these operations with code. This may be a bit difficult for the novice, but for experienced developers it is quicker to find the approximate solution and then improve it.

The first thing I think about is that I used to build 2D bricks on my phone. It's just three steps to dismantle our operation action:

1. First press the finger or mouse, find the initial coordinates, let the object dynamically appear in the coordinate position.

2. Then judge the movement, and let the object follow the mouse or finger movement.

3. Finally lift the finger or mouse, let the object fixed in the final coordinate position, the coordinate data written to the text or database.

To figure out the three steps, we are in the bottom of the heart, we just have to implement the three-step operation code, basically object placement function can be achieved.

When I realized the first step, I was having a problem. I was 2D when I was writing a block game, and the coordinates were very good to get. But in "The troubled: Homeland", I want to get the mouse on the ground coordinate point ah. I started with two lines of code:

Vector3 mouseposition= input.mouseposition;    // get the coordinates of your mouse  // turn The coordinates of the mouse into the space coordinates of the 3D game world

I thought that my train of thought is no problem, the result runs the code to look. Haha, the building is not on the ground at all Ah, is in the air ah. Later checked the relevant documents to know that the mouse position is the mouse on the screen on this three-dimensional surface position (you can imagine the screen is a transparent three-dimensional wall, the wall is set on the ground). To be more professional, it is from the main camera as a starting point, the mouse refers to the end of a ray, and the screen is the point where the stereo intersection. For the intuitive feel of this stereoscopic surface, you can see the 2D UI in 3D space on the side of the interface.

Therefore, the above two lines of code are not able to solve the problem. But with such an analysis, I was exposed to the concept of radiation. I thought to myself, if I could find the focal point of this ray and the ground, it would be all right! Our thinking becomes the following pseudo-code:

1. Create a ray Ray that starts from the primary camera and points to the end of the mouse.

2. Find the intersection of Ray and ground terrain position.

3. Dynamically create the coordinates of the object in the position.

The core code is as follows:

if (Input.getmousebutton (0)) {  = Camera.main.ScreenPointToRay (input.mouseposition);       Raycasthit hit;        if  out Hit ))       {               if(hit.transform.name=="Terrain")               {                           = Hit.point; // get the coordinates of the collision point with the ground                }         }}

After solving this core problem, the moving code is very well written. As long as the finger or mouse movement, the dynamic update of the object's coordinates for the new position can be. The code for rotating objects can also be written together to dynamically update the rotation of the object. This code is very easy to write, I do not post it, the Novice can exercise self-action ability. Old hands don't have to look in the eye. It is of course necessary to remind that this code needs to be run in the update function.

Finally, the code of the fixed object coordinates becomes the final position recorded in the text or written to the database. This code is not very difficult to write.

Finally, we look at the whole idea of solving the problem:

Procedure---decomposition procedure--complete and correct code after decomposition with code

If you ask me for the entire code, frankly: I think "give people to fish, rather than grant to the fishing." You can get the right solution to the problem, and then remember the core code. To be a programmer to a "hands without swords, the heart has a sword" state is enough. Of course, the novice or more practice sword, gestures and gestures under the moves.

PS: Game demo Group: 198035671 Unity3d Technology Exchange Group: 308185833 Fish Game Development Live address: Www.douyutv.com/unity3d

Unity3d Independent game Development Diary (ii): placing buildings

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.