当先锋百科网

首页 1 2 3 4 5 6 7

这个是控制相机:(44条消息) unity 相机 旋转缩放查看 物体或地图_unity旋转查看物体_野区捕龙为宠的博客-CSDN博客

下面的是控制物体本身:

知识点:

Input.GetMouseButton(0)
获取鼠标输入,参数为一个int值
为0的时候获取的是左键
Input.GetMouseButton(1)
为1的时候获取的是右键
Input.GetMouseButton(2)
为2的时候获取的是中键(就是那个滑轮)
Input.GetMouseButton
鼠标点击
Input.GetMouseButtonUp
鼠标松开
Input.GetMouseButtonDown
鼠标按压
Camera.main.ScreenToWorldPoint
屏幕坐标转化为世界坐标
Quaternion rotation = Quaternion.Euler(0, 0, 0);
欧拉角转化为四元数

Input.GetKey(KeyCode.Mouse2)//按下滚轮键

尊重原著:Unity鼠标控制物体的旋转、移动、缩放等_unity 获取鼠标移动距离_Cuijiahao的博客-CSDN博客



方法一:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class MouseControlModel : MonoBehaviour
{
    //旋转最大角度
    public int yMinLimit = -20;
    public int yMaxLimit = 80;
    //旋转速度
    public float xSpeed = 250.0f;
    public float ySpeed = 120.0f;
    //旋转角度
    private float x = 0.0f;
    private float y = 0.0f;
 
    void Update()
    {
        if (Input.GetMouseButton(0))
        {
            //将屏幕坐标转化为世界坐标  ScreenToWorldPoint函数的z轴不能为0,不然返回摄像机的位置,而Input.mousePosition的z轴为0
            //z轴设成10的原因是摄像机坐标是(0,0,-10),而物体的坐标是(0,0,0),所以加上10,正好是转化后物体跟摄像机的距离
            Vector3 temp = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10));
            transform.position = temp;
        }
        else if (Input.GetMouseButton(1))
        {
            //Input.GetAxis("MouseX")获取鼠标移动的X轴的距离
            x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
            y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
            y = ClampAngle(y, yMinLimit, yMaxLimit);
            //欧拉角转化为四元数
            Quaternion rotation = Quaternion.Euler(y, x, 0);
            transform.rotation = rotation;
        }
        else if (Input.GetAxis("Mouse ScrollWheel") != 0)
        {
            //鼠标滚动滑轮 值就会变化
            if (Input.GetAxis("Mouse ScrollWheel") < 0)
            {
                //范围值限定
                if (Camera.main.fieldOfView <= 100)
                    Camera.main.fieldOfView += 2;
                if (Camera.main.orthographicSize <= 20)
                    Camera.main.orthographicSize += 0.5F;
            }
            //Zoom in  
            if (Input.GetAxis("Mouse ScrollWheel") > 0)
            {
                //范围值限定
                if (Camera.main.fieldOfView > 2)
                    Camera.main.fieldOfView -= 2;
                if (Camera.main.orthographicSize >= 1)
                    Camera.main.orthographicSize -= 0.5F;
            }
        }
    }
 
    //角度范围值限定
    static float ClampAngle(float angle, float min, float max)
    {
        if (angle < -360)
            angle += 360;
        if (angle > 360)
            angle -= 360;
        return Mathf.Clamp(angle, min, max);
    }
}
 

方法二:

尊重原著:Unity中通过鼠标实现移动、缩放、旋转_unity 鼠标旋转缩放_rich22851716的博客-CSDN博客

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

public class MouseControlModel02 : MonoBehaviour
{
    public float RotateXSpeed = 1;//旋转速度
    public float RotateYSpeed = 1;

    public float xSpeed = 1;//移动位置速度
    public float ySpeed = 1;
    float posX;
    float posY;
    public float translateSensity = 1;
    float mousX;
    float mousY;
    public float nomalscale = 0.5f;
    public float MouseWheelSensitivity = 0.9f;//放大缩小  速度
    public float MouseZoomMinx = 0.2f;
    public float MouseZoomMax = 2f;
    protected GameObject obj;
    public bool isRotate = true;
    void Start()
    {

    }
    void FixedUpdate()
    {
        posX = this.gameObject.transform.localPosition.x;
        posY = this.gameObject.transform.localPosition.y;

        if (this.gameObject.transform.localScale != new Vector3(0.000001f, 0.000001f, 0.000001f))
        {
            if (Input.GetMouseButton(1))
            {
                if (isRotate)
                {
                    mousX = Input.GetAxis("Mouse X");
                    mousY = Input.GetAxis("Mouse Y");
                    this.transform.Rotate(mousY * RotateYSpeed, -mousX * RotateXSpeed, 0, Space.World);
                }
                else
                {
                }
            }

            if (Input.GetAxis("Mouse ScrollWheel") != 0)
            {
                if (nomalscale >= MouseZoomMinx && nomalscale <= MouseZoomMax)
                {
                    nomalscale += Input.GetAxis("Mouse ScrollWheel") * MouseWheelSensitivity;
                }
                if (nomalscale < MouseZoomMinx)
                {
                    nomalscale = MouseZoomMinx;
                }
                if (nomalscale > MouseZoomMax)
                {
                    nomalscale = MouseZoomMax;
                }
                this.transform.localScale = new Vector3(nomalscale, nomalscale, nomalscale);
            }
            if (Input.GetKey(KeyCode.Mouse2))
            {
                posX += Input.GetAxis("Mouse X") * xSpeed * translateSensity;
                posY += Input.GetAxis("Mouse Y") * ySpeed * translateSensity;
                Vector3 pos = new Vector3(posX, posY, this.gameObject.transform.localPosition.z);
                this.gameObject.transform.localPosition = pos;
            }
        }
    }
}