Home > other >  Unity 3 d game small mapping problem
Unity 3 d game small mapping problem

Time:09-22

Public class smallMap: MonoBehaviour {
//large map terrain objects
GameObject plane;
//leading large map object
GameObject cube;

//the width of the large map
Float mapWidth;
//the height of the large map
Float mapHeight;
//map boundary detection value
Float widthCheck;
Float heightCheck;

//small map the location of the protagonist
Float mapcube_x=0;
Float mapcube_y=0;


//GUI button is pressed a
Bool keyUp;
Bool keyDown;
Bool keyLeft;
Bool keyRight;

//the background of the small map map
Public Texture map.
//in the role of small map map
Public Texture map_cube;

Void the Start ()
{
//get large map object
Plane=GameObject. Find (" plane ");
//get leading large map object
Cube=GameObject. Find (" cube ");
//get large map default width
Float size_x=plane. GetComponent (a). Mesh. Bounds. Size. X;
//get a large map width scaling
Float scal_x=plane. The transform. LocalScale. X.
//get large map the default height
Float size_z=plane. GetComponent (a). Mesh. Bounds. Size. Z;
//get large map zoom geographic
Float scal_z=plane. The transform. LocalScale. Z;

//the original width multiplied by scaling to calculate the real width
MapWidth=size_x * scal_x;
MapHeight=size_z * scal_z;

//the width of the cross-border monitoring
WidthCheck=mapWidth/2;
HeightCheck=mapHeight/2;

Check ();
}

Void OnGUI ()
{
KeyUp=GUILayout. RepeatButton (" move ");

KeyDown=GUILayout. RepeatButton (" backward ");

KeyLeft=GUILayout. RepeatButton (" move to the left ");

KeyRight=GUILayout. RepeatButton (" to the right ");

//map small background
GUI. DrawTexture (new the Rect (Screen width - map. Width of 0, the map. The width of the map. The height), map);
//draw the minimap "leading role"
GUI. DrawTexture (new the Rect (mapcube_x, mapcube_y, map_cube. Width, map_cube height), map_cube);
}

Void FixedUpdate ()
{


If (keyUp)
{
//move
Cube. Transform. Translate (Vector3. Forward * Time. DeltaTime * 5);
Check ();

}

If (keyDown)
{
//move back
Cube. Transform. Translate (- Vector3. Forward * Time. DeltaTime * 5);
Check ();
}

If (keyLeft)
{
//to the
Cube. Transform. Translate (- Vector3. Right * Time. DeltaTime * 5);
Check ();
}

If (keyRight)
{
//to the right
Cube. Transform. Translate (Vector3. Right * Time. DeltaTime * 5);
Check ();
}




}

//cross test
Void the check ()
{
//get the leading role in the map coordinates,
Float x=cube. The transform. The position. X;
Float z=cube. The transform. The position. Z;

//when the protagonist than map control, leading to recalculate the coordinates
If (x & gt;=widthCheck)
{
X=widthCheck;
}
If (x & lt;=- widthCheck)
{
X=- widthCheck;
}
If (z & gt;=heightCheck)
{
Z=heightCheck;
}
If (z & lt;=- heightCheck)
{
Z=- heightCheck;
}

Cube. The transform. The position=new Vector3 (x, cube. The transform. The position, y, z);

//on a proportional basis "leading role" small map coordinates
//GUI coordinates is the top left corner as the starting point and the screen coordinates the bottom left corner as the starting point
Mapcube_x=(map width/mapWidth * x) + ((map. Width/2) - (map_cube. Width/2) + (Screen. Width - map. Width);
Mapcube_y=map. The height - ((map. The height/mapHeight * z) + (map. Height/2));
//mapcube_y=((map. Height/mapHeight * z) + (map. Height/2));
}
}
Protagonist extremely small don't understand the last for the map coordinates of the two lines of code, strives for the great god to do a detailed explanation!!!!!!

CodePudding user response:

Not a great god willing to answer this question
  • Related