unity 不能隐式地将类型 float' 转换为 bool' 也使相机变焦?
unity cannot implicitly convert type float' to bool' also Make the camera zoom?
我使用 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"))
上时,我收到错误消息“无法将类型 float 隐式转换为 bool任何关于如何使缩放程序工作的建议将不胜感激。
Input.GetAxis("Mouse ScrollWheel");
脚本return一个浮点值。
The value will be in the range -1...1 for keyboard and joystick input.
If the axis is setup to be delta mouse movement, the mouse delta is
multiplied by the axis sensitivity and the range is not -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);
我使用 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"))
上时,我收到错误消息“无法将类型 float 隐式转换为 bool任何关于如何使缩放程序工作的建议将不胜感激。
Input.GetAxis("Mouse ScrollWheel");
脚本return一个浮点值。
The value will be in the range -1...1 for keyboard and joystick input. If the axis is setup to be delta mouse movement, the mouse delta is multiplied by the axis sensitivity and the range is not -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);