zoukankan      html  css  js  c++  java
  • 小地图

    using UnityEngine;
    using System.Collections;
    
    public class CreateMap : MonoBehaviour {
    
        protected GameObject plane;
        protected GameObject cube;
    
        protected float mapWidth;
        protected float mapHeight;
        protected float widthCheck;
        protected float heightCheck;
    
        protected float mapCube_x=0;
        protected float mapCube_y=0;
    
        public Texture map;
        public Texture map_cube;
    
        // Use this for initialization
        void Start () {
            plane =GameObject.Find("Plane");
            cube=GameObject.Find("Cube");
            float size_x=plane.GetComponent<MeshFilter>().mesh.bounds.size.x; //默认宽度
            float scale_x=plane.transform.localScale.x; //默认缩放比例
            float size_z=plane.GetComponent<MeshFilter>().mesh.bounds.size.z;
            float scale_z=plane.transform.localScale.z;
    
            //原始宽度*缩放比例=实际宽度
            mapWidth=size_x*scale_x;
            mapHeight=size_z*scale_z;
    
            widthCheck =mapWidth/2;
            heightCheck =mapHeight/2;
    
            check();
    
        }
    
        void check()
        {
            float x=cube.transform.position.x;
            float z=cube.transform.position.z;
            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);
    
            //小地图主角的坐标
            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));
    
        }
        // Update is called once per frame
        void Update () {
            if(Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
            {
                cube.transform.Translate(Vector3.forward*Time.deltaTime *5);
                check();
            }
            if(Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
            {
                cube.transform.Translate(Vector3.forward * (-Time.deltaTime) *5);
                check();
            }
        }
    
        void OnGUI()
        {
            GUI.DrawTexture(new Rect(Screen.width-map.width,0,map.width,map.height),map);
            GUI.DrawTexture(new Rect(mapCube_x,mapCube_y,map_cube.width,map_cube.height),map_cube);
        }
    }
  • 相关阅读:
    [Python]小甲鱼Python视频第038课(类和对象:继承 )课后题及参考解答
    [Python]小甲鱼Python视频第037课(类和对象:面向对象编程 )课后题及参考解答
    [Python]小甲鱼Python视频第036课(类和对象:给大家介绍对象 )课后题及参考解答
    Session共享
    防止表单重复提交
    Docker安装redis
    Docker安装mysql
    docker-compose常用命令
    Docker常用命令
    Oracle 随机取记录
  • 原文地址:https://www.cnblogs.com/hometown/p/3927599.html
Copyright © 2011-2022 走看看