像Unity3d中的3ds Max一样缩放到鼠标位置 [英] Zoom to mouse position like 3ds Max in Unity3d

查看:83
本文介绍了像Unity3d中的3ds Max一样缩放到鼠标位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要实现的是能够使用鼠标滚轮将相机缩放到我的鼠标所在的位置.认为它更像3ds max如何做到这一点.相机会放大到鼠标所在的位置,并且不会出现抽动或捕捉.

What I want to achieve is to be able to zoom the camera using the mouse wheel to where my mouse is. Think of it more like how 3ds max does this. The camera zooms into the point where the mouse is and there are no jerks or snaps.

这是我当前的实现,但是这假设目标位于中间.如果我移动目标位置,则相机会捕捉到该位置,并基本上将其带到中心.

This is my current implementation but this assumes the target is in the center. If I move the target position the camera snaps to that position and basically brings it to the center.

targetDist -= Input.GetAxis("Mouse ScrollWheel") * zoomSpeed * 0.5f * (isOrtho ? 15f : 1f);
targetDist = Mathf.Max(0.1f,targetDist);
distance = moveSmoothing * targetDist + (1-moveSmoothing )*distance;

transform.localRotation = Quaternion.Slerp( transform.localRotation, targetRot, rotateSmoothing * deltaTimeFactor );

target.position = Vector3.Lerp(target.position, targetLookAt, moveSmoothing * deltaTimeFactor);
distanceVec.z = distance;

transform.position = target.position - transform.rotation * distanceVec;

有什么想法吗?

推荐答案

所以我设法弄清楚了.对于任何想要这个的人.此脚本已应用在相机"上,并且在更新"功能中.

So I got around to figuring it out. For anyone looking for this. This script is applied on the Camera and this is in the Update function.

if (Input.GetAxis("Mouse ScrollWheel") != 0)
{
    RaycastHit hit;
    Ray ray = this.transform.GetComponent<Camera>().ScreenPointToRay(Input.mousePosition);
    Vector3 desiredPosition;

    if (Physics.Raycast(ray , out hit))
    {
        desiredPosition = hit.point;
    }
    else
    {
        desiredPosition = transform.position;
    }
    float distance = Vector3.Distance(desiredPosition , transform.position);
    Vector3 direction = Vector3.Normalize( desiredPosition - transform.position) * (distance * Input.GetAxis("Mouse ScrollWheel"));

    transform.position += direction;
}

这篇关于像Unity3d中的3ds Max一样缩放到鼠标位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆