c# - 通过变换而不是 fov 使用鼠标滚轮放大/缩小相机?

标签 c# unity3d

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

public class MouseOrbit : MonoBehaviour {

    public Transform target;
    public float distance = 5.0f;
    public float xSpeed = 120.0f;
    public float ySpeed = 120.0f;

    public float yMinLimit = -20f;
    public float yMaxLimit = 80f;

    public float distanceMin = .5f;
    public float distanceMax = 15f;

    private Rigidbody rigidbody;

    float x = 0.0f;
    float y = 0.0f;

    float minFov = 15f;
    float maxFov = 90f;
    float sensitivity = 10f;

    // Use this for initialization
    void Start()
    {
        Vector3 angles = transform.eulerAngles;
        x = angles.y;
        y = angles.x;

        rigidbody = GetComponent<Rigidbody>();

        // Make the rigid body not change rotation
        if (rigidbody != null)
        {
            rigidbody.freezeRotation = true;
        }
    }

    void Update()
    {
        // Updating camera distance on every frame
        distance = RayCast3.distance3;

        //Setting maximum distance so the camera doesnt go too far
        if (distance > 2)
        {
            distance = 2;
        }
    }

    void LateUpdate()
    {
        if (target)
        {
            x += Input.GetAxis("Mouse X") * xSpeed * distance * 0.02f;
            y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;

            y = ClampAngle(y, yMinLimit, yMaxLimit);

            Quaternion rotation = Quaternion.Euler(y, x, 0);

            //distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel") * 5, distanceMin, distanceMax);
            //distance += Input.GetAxis("Mouse ScrollWheel") * sensitivity;
            float fov = Camera.main.fieldOfView;
            fov += Input.GetAxis("Mouse ScrollWheel") * sensitivity;
            fov = Mathf.Clamp(fov, minFov, maxFov);
            Camera.main.fieldOfView = fov;

            RaycastHit hit;
            if (Physics.Linecast(target.position, transform.position, out hit))
            {
                distance -= hit.distance;
            }
            Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
            Vector3 position = rotation * negDistance + target.position;

            transform.rotation = rotation;
            transform.position = position;
        }
    }

    public static float ClampAngle(float angle, float min, float max)
    {
        if (angle < -360F)
            angle += 360F;
        if (angle > 360F)
            angle -= 360F;
        return Mathf.Clamp(angle, min, max);
    }
}

现在我正在使用 fov: 并且运行良好。

float fov = Camera.main.fieldOfView;
fov += Input.GetAxis("Mouse ScrollWheel") * sensitivity;
fov = Mathf.Clamp(fov, minFov, maxFov);
Camera.main.fieldOfView = fov;

但现在我想使用距离变量而不是 fov。 所以我首先尝试了这一行:

distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel") * 5, distanceMin, distanceMax);

它没有用,所以我尝试了这条线:

distance += Input.GetAxis("Mouse ScrollWheel") * sensitivity;

但在这两行中,角色都断断续续,并且没有用鼠标滚轮放大。

最佳答案

其实很简单。获取鼠标滚轮速度,然后将其乘以某个速度值。如果需要,您还可以将它乘以 Time.deltaTime。最后使用 transform.Translate 以该值移动相机。

这将在 z 轴上移动:

private float zoomSpeed = 2.0f;

void Update()
{

    float scroll = Input.GetAxis("Mouse ScrollWheel");
    transform.Translate(0, 0, scroll * zoomSpeed, Space.World);
}

或者相机朝向的地方:

private float zoomSpeed = 5.0f;

void Update()
{

    float scroll = Input.GetAxis("Mouse ScrollWheel");
    transform.position += this.transform.forward * scroll * zoomSpeed;
}

关于c# - 通过变换而不是 fov 使用鼠标滚轮放大/缩小相机?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43936888/

相关文章:

c# - 什么 C# 数据类型应该用于部分常量信息集?

c# - 线程安全的单元测试?

c# - 初始化时触发自定义 DependencyProperty 的 PropertyChangedCallback

ios - 如何在 Unity 中手动关闭 TouchScreenKeyboard

c# - 如何在 Unity android 项目中制作自定义 firebase 事件?

c# - 将 Int 数组转换为枚举标志

c# - ADO.NET DataSet.Load() 似乎无法正确处理空表

c# - HoloLens -- 在 Unity 中拍摄照片并保存到磁盘

unity3d - 如何在 Unity3D 中绘制圆?

android - Admob:在用户关闭广告之前添加计时器