gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 21:02:33 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com