美文网首页
缩放的代码,缩放、自动旋转、拖拽物体的综合:

缩放的代码,缩放、自动旋转、拖拽物体的综合:

作者: 8fc08e44cf80 | 来源:发表于2017-01-06 13:32 被阅读0次

    usingUnityEngine;

    usingSystem.Collections;

    /**

    * @Function:使物体自动旋转,鼠标能够360°拖转物体,托转物体的时候,自动旋转停止,同时滚轮实现物体的缩放功能

    * @Ahthor:黄杰

    * @Date:2013-04-24

    */

    publicclassZoomAndDrag : MonoBehaviour {

    publicCamera MainCamera;

    publicfloatZoomMin;//滚轮的最小值

    publicfloatZoomMax;//滚轮的最大值

    privatefloatnormalDistance;//设置摄像机的景深值

    privatefloatMouseWheelSencitivity = 10.0f;//鼠标灵敏度,就是缩放的速度的快慢

    privatefloataxisX;

    privatefloataxisY;

    publicfloatspeed = 6f;

    privatefloattempSpeed;

    privateboolRoationOnly;

    voidStart ()

    {

    normalDistance = 50.0f;

    ZoomMin = 20.0f;

    ZoomMax = 100.0f;

    RoationOnly =true;

    }

    voidUpdate ()

    {

    Roation();

    Zoom();

    this.transform.Rotate(newVector3(axisY, axisX, 0) * Rigid(), Space.World);//物体旋转的方法

    }

    //自动旋转物体的方法,放在Update中调用

    voidRoation()

    {

    if(RoationOnly)

    {

    gameObject.transform.Rotate(Vector3.up * Time.deltaTime * 10);

    }

    }

    /****

    *鼠标滚轮缩放物体的方法

    *

    * **/

    voidZoom()

    {

    if(Input.GetAxis("Mouse ScrollWheel") != 0)

    {

    if(normalDistance >= ZoomMin && normalDistance <= ZoomMax)

    {

    normalDistance -= Input.GetAxis("Mouse ScrollWheel") * MouseWheelSencitivity;

    }

    if(normalDistance < ZoomMin)

    {

    normalDistance = ZoomMin;

    }

    if(normalDistance > ZoomMax)

    {

    normalDistance = ZoomMax;

    }

    MainCamera.fieldOfView = normalDistance;

    }

    }

    /***

    *

    * 鼠标左键控制物体360°旋转+惯性

    * **/

    floatRigid()

    {

    if(Input.GetMouseButton(0))

    {

    RoationOnly =false;//当鼠标按下的时候,停止自动旋转

    axisX = Input.GetAxis("Mouse X");

    axisY = Input.GetAxis("Mouse Y");

    if(tempSpeed < speed)

    {

    tempSpeed += speed * Time.deltaTime * 5;

    }

    else

    {

    tempSpeed = speed;

    }

    }

    else

    {

    RoationOnly =true;//当鼠标没有按下的时候,恢复自动旋转

    if(tempSpeed > 0)

    {

    tempSpeed -= speed * Time.deltaTime;

    }

    else

    {

    tempSpeed = 0;

    }

    }

    returntempSpeed;

    }

    }

    相关文章

      网友评论

          本文标题:缩放的代码,缩放、自动旋转、拖拽物体的综合:

          本文链接:https://www.haomeiwen.com/subject/jqotbttx.html