您当前的位置: 首页 >  unity

程序员正茂

暂无认证

  • 3浏览

    0关注

    283博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Unity三维场景视图(缩放、平移、旋转)

程序员正茂 发布时间:2021-06-29 09:56:06 ,浏览量:3

鼠标滚轮:缩放

鼠标右键:旋转

鼠标中键:平移

鼠标左键:选中实体(下面的代码中未实现)

注:

1.地形必须在Terrain图层下

2.相机上必须挂载Rigidbody+Collider,防止钻到地下。

 Rigidbody的Interpolate:Interpolate

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class Viewer : MonoBehaviour
{
    public Texture2D RotateIcon;
    public Texture2D PanIcon;
    public Rigidbody rigibodyCamera;
    public bool IsRecoverCameraForword = false;

    // Start is called before the first frame update
    void Start()
    {

    }
    // Update is called once per frame
    void Update()
    {
        //鼠标是否在UI上
        if (EventSystem.current != null && EventSystem.current.IsPointerOverGameObject())
        {
            return;
        }

        //旋转视角
        if (Input.GetMouseButtonDown(1))
        {
            Cursor.SetCursor(RotateIcon, Vector3.zero, CursorMode.Auto);
        }

        if (Input.GetMouseButtonUp(1))
        {
            Cursor.SetCursor(null, Vector3.zero, CursorMode.Auto);
        }
        if (Input.GetMouseButton(1))
        {
            Vector3 ptCenter = rigibodyCamera.transform.position - rigibodyCamera.transform.forward * 0.1f;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hitInfo;
            LayerMask mask = 1  Mathf.Abs(axis_y))
                {
                    float drag_x_speed = Mathf.Rad2Deg * (radius / dist);
                    float rotX = axis_x * drag_x_speed * Time.deltaTime;
                    rigibodyCamera.transform.RotateAround(ptCenter, Vector3.up, rotX);
                }
                else
                {
                    float drag_y_speed = Mathf.Rad2Deg * (radius / dist);
                    float rotY = axis_y * drag_y_speed * Time.deltaTime;
                    rigibodyCamera.transform.RotateAround(ptCenter, -rigibodyCamera.transform.right, rotY);
                }
            }
            //鼠标没点在地面上,自由旋转
            else
            {
                float axis_x = Input.GetAxis("Mouse X");
                float axis_y = Input.GetAxis("Mouse Y");
                if (Mathf.Abs(axis_x) > Mathf.Abs(axis_y))
                {
                    float drag_x_speed = 50.0f;
                    float rotX = axis_x * drag_x_speed * Time.deltaTime;
                    rigibodyCamera.transform.RotateAround(ptCenter, Vector3.up, rotX);
                }
                else
                {
                    float drag_y_speed = 30.0f;
                    float rotY = axis_y * drag_y_speed * Time.deltaTime;
                    rigibodyCamera.transform.RotateAround(ptCenter, -rigibodyCamera.transform.right, rotY);
                }
            }
        }

        //放大
        if (Input.GetAxis("Mouse ScrollWheel") > 0)
        {   
            float speed = 50.0f;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hitInfo;
            LayerMask mask = 1             
关注
打赏
1660743125
查看更多评论
0.0378s