个人技术总结

Unity3D引擎C#脚本实现视角拖动

项目需求是要我做一个能用鼠标拖动画面的脚本
我的思路就是记录鼠标一开始点击的位置,然后根据相对位移,反向更改Main Camare的坐标来实现视角的移动从而实现画面拖动。

变量定义(一会要绑上控件的)

    public BoxCollider2D Bounds = null; //移动的边界
    public Vector3 deceleration = new Vector3(1,1,0);//减速度
    public Vector3
        minVec3,
        maxVec3;
    private Vector2 beginP = Vector2.zero;//鼠标第一次落下点  
    private Vector2 endP = Vector2.zero;//鼠标第二次位置(拖拽位置)  
    private Vector3 speed = Vector3.zero;
    public Camera eyeCamera = null; // 视图相机
    public bool isUpdateTouch = true; //是否更新touch 

脚本开始时的赋值

void Start()
{
    if (eyeCamera == null) {
        eyeCamera = Camera.main;
    }
    if (Bounds) {
        minVec3 = Bounds.bounds.min;//包围盒  
        maxVec3 = Bounds.bounds.max;
    }
}

三种状态的鼠标坐标

///初始化位置,为接下来的move做准备
    void MoveBegin(Vector3 point) {
        beginP = point;
        speed = Vector3.zero;
    }
    ///更新目标位置
    void Moveing(Vector3 point)
    {
        //记录鼠标拖动的位置     
        endP = point;
        Vector3 fir = eyeCamera.ScreenToWorldPoint(new Vector3(beginP.x, beginP.y, eyeCamera.nearClipPlane));//转换至世界坐标  
        Vector3 sec = eyeCamera.ScreenToWorldPoint(new Vector3(endP.x, endP.y, eyeCamera.nearClipPlane));
        speed = sec - fir;//需要移动的 向量  
    }
    ///Move结束,清除数据
    void MoveEnd(Vector3 point)
    {
        MoveBegin(point);
    }

移动Main Camare坐标(spead为移动的速度,实际效果类似灵敏度)

void Update()
    {
        if (Input.GetAxis("Mouse ScrollWheel") <0)  
        {  
           if(Camera.main.fieldOfView<=50)  
           Camera.main.fieldOfView +=2;  
           if(Camera.main.orthographicSize<=4.5)  
           Camera.main.orthographicSize +=0.5F;  
        }  
        //Zoom in  
        if (Input.GetAxis("Mouse ScrollWheel") > 0)  
        {  
          if(Camera.main.fieldOfView>20)  
            Camera.main.fieldOfView-=2;  
          if(Camera.main.orthographicSize>=2)  
            Camera.main.orthographicSize-=0.5F;  
        }
        if (speed == Vector3.zero)
        {
            return;
        }
        var x = transform.position.x;
        var y = transform.position.y;
        x = x - speed.x;//向量偏移  
        y = y - speed.y;

        if (Bounds)
        {
            float cameraHeight = Camera.main.orthographicSize * 2;
            var cameraSize = new Vector2(Camera.main.aspect * cameraHeight, cameraHeight);
            var cameraHalfWidth = eyeCamera.orthographic ?cameraSize.x / 2 : 0;
            var cameraHalfHeight = eyeCamera.orthographic ?cameraSize.y / 2 : 0;
            //保证不会移除包围盒  

            x = Mathf.Clamp(x, minVec3.x + cameraHalfWidth, maxVec3.x - cameraHalfWidth);
            y = Mathf.Clamp(y, minVec3.y + cameraHalfHeight, maxVec3.y - cameraHalfHeight);
        }
        transform.position = new Vector3(x, y, transform.position.z);

        if (System.Math.Abs(speed.x) < 0.01f)
        {
            speed.x = 0;
        }
        else
        {
            if (speed.x > 0)
            {
                speed.x -= deceleration.x * Time.deltaTime;
                if (speed.x < 0) {
                    speed.x = 0;
                }
            }
            else
            {
                speed.x += deceleration.x * Time.deltaTime;
                if (speed.x > 0)
                {
                    speed.x = 0;
                }
            }
        }
        if (System.Math.Abs(speed.y) < 0.01f)
        {
            speed.y = 0;
        }
        else
        {
            if (speed.y > 0)
            {
                speed.y -= deceleration.y * Time.deltaTime;
                if (speed.y < 0)
                {
                    speed.y = 0;
                }
            }
            else
            {
                speed.y += deceleration.y * Time.deltaTime;
                if (speed.y > 0)
                {
                    speed.y = 0;
                }
            }
        }
        beginP = endP;
        if (speed.x == 0 && speed.y == 0)
        {
            speed = Vector3.zero;
        }
    }

在Unity上的控件绑定

为了更好的游戏体验我还增加了个用滚轮放大缩小画面的脚本
原理是滚轮事件改变Main Camare高度

    if (Input.GetAxis("Mouse ScrollWheel") <0)  
        {  
           if(Camera.main.fieldOfView<=50)  
           Camera.main.fieldOfView +=2;  
           if(Camera.main.orthographicSize<=4.5)  
           Camera.main.orthographicSize +=0.5F;  
        }  
        if (Input.GetAxis("Mouse ScrollWheel") > 0)  
        {  
          if(Camera.main.fieldOfView>20)  
            Camera.main.fieldOfView-=2;  
          if(Camera.main.orthographicSize>=2)  
            Camera.main.orthographicSize-=0.5F;  
        }

遇到的难题就是画面可以无限拖,所以我加了一个不可见的物理精灵碰撞盒作为边界,这样就不会超出去了

进行总结

Unity3d是一个功能很强大的引擎,里面有很多很方便的控件和属性,很有意思,不过卡牌游戏不能充分体现他的强大,物体碰撞那块用的很少,很可惜。

列出参考文献、参考博客

B站的从零开始Uinty教程
Unity官方问答
简书

posted @ 2021-06-27 22:02  murasameEM  阅读(55)  评论(0编辑  收藏  举报