[Unity 3D] Study Notes 29th: Game instance-simple small map creation

Source: Internet
Author: User

It is hard to learn anything without training. So this time I will summarize how to make small maps in mmropg games. In mmropg games, when the main character moves around in the game world, a small map of the current game scenario is usually displayed in the upper-right corner of the screen. The main character moves around in the game world, and the small mark representing the main character in the map will also move. How can this problem be achieved?

First, you need to determine two textures. The first one is the background map in the upper right corner. It should be a large map of the main character's position down from the Y axis. The second is the location of the main character. In this example, because you have not learned how to create a unity map, the map is replaced by a surface object, the protagonist is replaced by a cube, and the GUI is used to control the movement of the protagonist. In the upper-right corner of the map, the red rectangle in the middle represents the main character. The zoom ratio is calculated based on the width and height of the small map and the actual width and height of the map. Then, the position of the main character in the small map is determined based on the zoom ratio.

Code:

Using unityengine; using system. collections; public class script_04_17: monobehaviour {// map terrain object gameobject plane; // map main object gameobject cube; // map width float mapwidth; // float mapheight of a large map; // float widthcheck; float heightcheck; // float mapcube_x = 0; float mapcube_y = 0; // whether the GUI button is pressed by bool keyup; bool keydown; bool keyleft; bool keyright; // The background map of the small map public texture map; // The main texture of the small map public texture map_cube; void start () {// obtain the map object plane = gameobject. find ("plane"); // obtain the ing Object Cube = gameobject. find ("cube"); // obtain the default width of the map, float size_x = plane. getcomponent <meshfilter> (). mesh. bounds. size. x; // get the scaled ratio float scal_x = plane of the large map width. transform. localscale. x; // get the default height float size_z = plane. getcomponent <meshfilter> (). mesh. bounds. size. z; // get the map height to scale the geographic float scal_z = plane. transform. localscale. z; // The original width is multiplied by the zoom ratio to calculate the actual width. mapwidth = size_x * scal_x; mapheight = size_z * scal_z; // The width of out-of-bounds monitoring widthcheck = mapwidth/2; heightcheck = mapheight/2; check ();} void ongui () {keyup = guilayout. repeatbutton ("moving forward"); keydown = guilayout. repeatbutton ("move backward"); keyleft = guilayout. repeatbutton ("move to left"); keyright = guilayout. repeatbutton ("Move to the right"); // draws a small map background GUI. drawtexture (New rect (screen. width-map. width, 0, map. width, map. height), MAP); // draw the "main character" GUI on the small map. drawtexture (New rect (mapcube_x, mapcube_y, map_cube.width, map_cube.height), map_cube);} void fixedupdate () {If (keyup) {// move the cube forward. transform. translate (vector3.forward * time. deltatime * 5); check ();} If (keydown) {// move the cube backward. transform. translate (-vector3.forward * time. deltatime * 5); check ();} If (keyleft) {// move the cube to the left. transform. translate (-vector3.right * time. deltatime * 5); check ();} If (keyright) {// move cube to the right. transform. translate (vector3.right * time. deltatime * 5); check () ;}// void check () {// obtain the coordinate float x = cube of the current protagonist in the map. transform. position. x; float z = cube. transform. position. z; // when the control of the main character exceeds the MAP range, re-calculate the coordinate of the main character if (x> = widthcheck) {x = widthcheck;} If (x <=-widthcheck) {x =-widthcheck;} If (Z >= heightcheck) {z = heightcheck;} If (z <=-heightcheck) {z =-heightcheck;} cube. transform. position = new vector3 (x, cube. transform. position. y, z); // calculate the coordinate mapcube_x = (map. width/mapwidth * x) + (map. width/2)-(map_cube.width/2) + (screen. width-map. width); mapcube_y = map. height-(map. height/mapheight * z) + (map. height/2 ));}}
Run:

Click the button:



This is just a very basic example. Later, we will learn how to use unity to create a map instead of a simple plane. However, the basic principle and IDEA are the same. We use two textures to calculate the protagonist position of a small map based on the ratio of the two images. Then pay attention to the border monitoring of the main character. That's it. In the future, the Unity study notes will be exposed to the knowledge of game elements.

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.