unity3D基础学习 通过判断鼠标点击的是否是目标物体,物体旋转,滑动滚轮缩放拉近视角

时间:2022-08-30 23:45:02

贴代码:

摄像机的拉近视角代码:

public Transform target;

    public float minFov = 15f;

    public float maxFov = 70f;

    public float sensitivity = 10f;

    void Start()

    {

        transform.LookAt(target);

    }

    void Update()

    {

        if (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl))

        {

            float fov = Camera.main.fieldOfView;

            fov -= Input.GetAxis("Mouse ScrollWheel") * sensitivity;

            fov = Mathf.Clamp(fov, minFov, maxFov);

            Camera.main.fieldOfView = fov;

        }

    }

主要是通过控制摄像机的fieldOfView属性,可以自己在场景视图中调节看一下。

通过判断选中物体是否是目标物体,如果是就旋转

代码:

public Transform target;//目标物体

    public float HorizontalSpeed = 10f;

    public float VerticalSpeed = 10f;

    string RotateDirection = null;//判断当前旋转方向,用于固定当前旋转方向,不让用户在横向旋转时纵向旋转,反之同理。

    bool isTarget = false;//判断当前射线碰撞到的物体是否为目标物体

    int RotateState = 0;//当前旋转状态0不旋转,1匀速旋转

  

    //GameObject PanelToolTips;

 

 void Start () {

       if(!target)

{

target=gameObject.transform;//如果target不存在,就设置脚本附加的物体为target

}

 }

 

 

 void Update () {

        if (Input.GetMouseButton(0))

        {

if (isTarget)//判断当前是否选中目标物体

            {

RotateState = 1;

                    RotateTarget(target);                       

            }

        }

      

        if (Input.GetMouseButtonUp(0))

        {

            RotateDirection = null;

            isTarget = false;//当鼠标抬起时,设置当前物体不是目标物体

            RotateState = 0;

        }

        if (Input.GetMouseButtonDown(0))//每次按下鼠标左键都用射线检测碰撞的物体是什么

        {

            Ray();

        }

      

 }

    /// <summary>

    /// 旋转匀速物体方法//

/// </summary>

    /// /// <param name="transformObj">要旋转的目标物体</param>

    void RotateTarget(Transform transformObj)

    {

        float h = Input.GetAxis("Mouse X");//右正左负

        float v = Input.GetAxis("Mouse Y");//上正下负

if ((Mathf.Abs(h) > Mathf.Abs(v)) && (RotateDirection != "down" && RotateDirection != "up"))//此处设置当物体横向旋转时不能纵向旋转,纵向旋转时不能横向旋转,如果不需要,可把&&后面删除,RotateDirection变量也可删除



        {

            if (h < 0)

            {

                transformObj.Rotate(Vector3.up, HorizontalSpeed);

                RotateDirection = "left";

            }

            else if (h > 0)

            {

                transformObj.Rotate(-Vector3.up, HorizontalSpeed);

                RotateDirection = "right";//当前旋转方向为右向

            }

            RotateState = 1;//匀速旋转

        }

        else if ((Mathf.Abs(h) < Mathf.Abs(v)) && (RotateDirection != "left" && RotateDirection != "right"))//同上

        {

            if (v < 0)

            {

                transformObj.RotateAround(target.transform.position, Vector3.left, VerticalSpeed);

                RotateDirection = "down";//当前旋转方向为下向

            }

            else if (v > 0)

            {

                transformObj.RotateAround(target.transform.position, Vector3.right, VerticalSpeed);

                RotateDirection = "up";//当前旋转方向为上向

            }

           

            RotateState = 1;

        }

        //print(RotateDirection);

    }

/// <summary>

    /// 从主相机发送射线,判断当前点击的物体是否是要旋转的物体

    /// </summary>

    void Ray()

    {

        Ray ray=Camera.main.ScreenPointToRay(Input.mousePosition);

        RaycastHit hit;

        GameObject hitObj=null;

        if (Physics.Raycast(ray, out hit, 100f))

        {

            hitObj = hit.collider.gameObject;

            print(hitObj.name);

            if ((hitObj.transform.name=target.name )//如果当前碰撞的物体是目标物体

            {

                target = hitObj.transform;

                isTarget = true;

            }

        }

    }