我用C#而且我是新手。我正在尝试为团结游戏制作缩放功能。这是我的代码(我到目前为止):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Zoom : MonoBehaviour
{
public Camera cam;
public float zoom_speed = 20f;
// Update is called once per frame
void Update ()
{
if(Input.GetAxis("Mouse ScrollWheel"))
{
cam.fieldOfView = zoom_speed;
}
}
}
但是,我收到错误“当我将鼠标悬停在if(Input.GetAxis("Mouse Scrollwheel"))
上时,无法将类型浮动隐式转换为bool。关于如何使缩放程序工作的任何建议都将非常感激。
Input.GetAxis("Mouse ScrollWheel");
脚本返回浮点值。
对于键盘和操纵杆输入,该值将在-1 ... 1的范围内。如果轴设置为增量鼠标移动,则鼠标增量乘以轴灵敏度,范围不是-1 ... 1。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Zoom : MonoBehaviour
{
public Camera cam;
public float zoom_speed = 20f;
// Update is called once per frame
void Update ()
{
float d = Input.GetAxis("Mouse ScrollWheel");
if (d > 0f)
{
//Positive value
//Scroll up
cam.fieldOfView += zoom_speed;
}
else if (d < 0f)
{
//Negative value
//Scroll down
cam.fieldOfView -= zoom_speed;
}
}
}
补充0xBFE1A8的答案,您甚至不需要使用if ... else语句,因为该值将在-1 ... 1范围内,只需将Input.GetAxis("Mouse ScrollWheel")
值乘以zoom_speed即可。我还建议您将视野夹在您定义的最小值和最大值之间。
float scroll = Input.GetAxis("Mouse ScrollWheel");
cam.fieldOfView = Mathf.Clamp(cam.fieldOfView + zoom_speed * scroll * Time.deltaTime, minFieldOfView, maxFieldOfView);