首页 > Unity3D专栏 > 【Unity3D】鼠标拉近旋转拖拽模型
2017
08-15

【Unity3D】鼠标拉近旋转拖拽模型

功能:

右键旋转

中间拖拽

滚动缩进

using UnityEngine;
using System.Collections;
public class D_MoveGameObject : MonoBehaviour
{
    public Transform target;
    private int MouseWheelSensitivity = 1;
    private int MouseZoomMin = 1;
    private int MouseZoomMax = 5;
    private float normalDistance = 3;
    private Vector3 normalized;
    private float xSpeed = 250.0f;
    private float ySpeed = 120.0f
    private int yMinLimit = -20;
    private int yMaxLimit = 80;
    private float x = 0.0f;
    private float y = 0.0f;
    private Vector3 screenPoint;
    private Vector3 offset;
    private Quaternion rotation = Quaternion.Euler(new Vector3(30f, 0f, 0f));
    private Vector3 CameraTarget;
    void Start()
    {
        CameraTarget = target.position;
        float z = target.transform.position.z - normalDistance;
        transform.position = rotation * new Vector3(transform.position.x, transform.position.y, z);
        transform.LookAt(target);
        var angles = transform.eulerAngles;
        x = angles.y;
        y = angles.x;
    }
    void Update()
    {
        if (Input.GetMouseButton(1))
        {
            x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
            y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
            y = ClampAngle(y, yMinLimit, yMaxLimit);
            var rotation = Quaternion.Euler(y, x, 0);
            var position = rotation * new Vector3(0.0f, 0.0f, -normalDistance) + CameraTarget;
            transform.rotation = rotation;
            transform.position = position;
        }
        else if (Input.GetAxis("Mouse ScrollWheel") != 0)
        {
            normalized = (transform.position - CameraTarget).normalized;
            if (normalDistance >= MouseZoomMin && normalDistance <= MouseZoomMax)
            {
                normalDistance -= Input.GetAxis("Mouse ScrollWheel") * MouseWheelSensitivity;
            }
            if (normalDistance < MouseZoomMin)
            {
                normalDistance = MouseZoomMin;
            }
            if (normalDistance > MouseZoomMax)
            {
                normalDistance = MouseZoomMax;
            }
            transform.position = normalized * normalDistance;
        }
        else if (Input.GetMouseButtonDown(2))
        {
            screenPoint = Camera.main.WorldToScreenPoint(target.transform.position);
            offset = target.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
        }
        if (Input.GetMouseButton(2))
        {
            Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
            Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
            target.transform.position = curPosition;
        }
        transform.LookAt(CameraTarget);
    }
    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);
    }
}

本文参考雨松MOMO大神的方法。

 



虚拟世界提醒您:如果您觉得本文不错,快快将这篇文章分享出去吧 ,感谢您的支持和关注,谢谢!
最后编辑:
作者:虚拟世界
专注互联网技术及资讯 技术研究中心|有相关Unity3D问题或者作事宜请联系我哦!
捐 赠如果您愿意花10块钱请我喝一杯咖啡的话,请用手机扫描二维码即可通过支付宝或者微信直接向我捐款,在此非常感谢您对虚拟世界的捐赠。

留下一个回复

你的email不会被公开。