Unity3D中场景的移动控制(使用键盘控制移动,鼠标控制场景方向)

使用方法:将该脚本挂载到主摄像(MainCamera)机上
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Control
{
	public class Ctrl_KeyAndMouseScenesMoving : MonoBehaviour
	{
        public float sensitivityX = 2F;                                        //X转动增量速度
        public float sensitivityY = 2F;                                        //y转动增量速度
        private float minimumY = -90F;                                          //Y轴转动限制
        private float maximumY = 90F;
        public float rotationY = 144F;                                            //y起始值
        public float MovingScreenSpeed = 1f;                                    //移动屏幕的速度
        float delta_x, delta_y, delta_z;                                        //计算移动量
        public float NearOrFromDistance = 8;                                    //主摄像机拉近拉远距离
        public float NearOrFromZoomSpeed = 20f;                                 //拉近拉远速度
        Quaternion rotation;                                                    //旋转四元素

        public float Speed = 25;                                                //前后左右的移动速度

        void Update()
        {

            if (Input.GetMouseButton(0))
            {
                //左键旋转屏幕
                {
                    float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;

                    rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
                    rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);

                    transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
                }

            }
          
            if (Input.GetAxis("Mouse ScrollWheel") != 0)
            {//滚轴拉近拉远
                delta_z = -Input.GetAxis("Mouse ScrollWheel") * NearOrFromZoomSpeed;
                transform.Translate(0, 0, -delta_z);
                NearOrFromDistance += delta_z;
            }
            if (Input.GetMouseButton(2))
            {//滚轴中间移动屏幕
                delta_x = Input.GetAxis("Mouse X") * MovingScreenSpeed;
                delta_y = Input.GetAxis("Mouse Y") * MovingScreenSpeed;
                rotation = Quaternion.Euler(0, transform.rotation.eulerAngles.y, 0);
                transform.position = rotation * new Vector3(-delta_x, -delta_y, 0) + transform.position;
            }
            //前后左右
            if (Input.GetKey(KeyCode.A))
            {
                transform.Translate(-Speed * Time.deltaTime, 0, 0, Space.Self);
            }
            if (Input.GetKey(KeyCode.D))
            {
                transform.Translate(Speed * Time.deltaTime, 0, 0, Space.Self);
            }
            if (Input.GetKey(KeyCode.W))
            {
                transform.Translate(0, 0, Speed * Time.deltaTime, Space.Self);
            }
            if (Input.GetKey(KeyCode.S))
            {
                transform.Translate(0, 0, -Speed * Time.deltaTime, Space.Self);
            }
            
            //升高降低镜头
            if (Input.GetKey(KeyCode.H))
            {
                transform.Translate(0, Speed * Time.deltaTime, 0);
            }
            if (Input.GetKey(KeyCode.N))
            {
                transform.Translate(0, -Speed * Time.deltaTime, 0);
            }
        }


    }//Class_end
}

你可能感兴趣的:(Unity基础)