unity3d鼠标滚轮实现摄像头拉近后退
voidUpdate(){target=GameObject.Find("new_offer");//滚轮设置相机与人物的距离.if(Input.GetAxis("Mou...
void Update ()
{
target = GameObject.Find("new_offer");
// 滚轮设置
相机与人物的距离.
if(Input.GetAxis("Mouse ScrollWheel") != 0)
{
theDistance = theDistance + Input.GetAxis("Mouse ScrollWheel") *
Time.deltaTime * ScrollKeySpeed;
}
// 鼠标中间滚动得到的值是不确定的,不会正好就是0,或
-10,当大于0时就设距离为0,小于MaxDistance就设置为MaxDistance
if(theDistance>0)
{
theDistance = 0;
}
if(theDistance < MaxDistance)
{
theDistance = MaxDistance;
}
transform.position =
target.transform.position;
SetDistance();
}
void Start
()
{
if (rigidbody)
{
rigidbody.freezeRotation =
true;
transform.position = target.transform.position;
}
}
//设置相机与人物之间的距离
void SetDistance()
{
transform.Translate(Vector3.forward * theDistance);
}
请问,运行时发现摄像头只是上下移动,并不实现拉近后退的效果,这是为什么? 展开
{
target = GameObject.Find("new_offer");
// 滚轮设置
相机与人物的距离.
if(Input.GetAxis("Mouse ScrollWheel") != 0)
{
theDistance = theDistance + Input.GetAxis("Mouse ScrollWheel") *
Time.deltaTime * ScrollKeySpeed;
}
// 鼠标中间滚动得到的值是不确定的,不会正好就是0,或
-10,当大于0时就设距离为0,小于MaxDistance就设置为MaxDistance
if(theDistance>0)
{
theDistance = 0;
}
if(theDistance < MaxDistance)
{
theDistance = MaxDistance;
}
transform.position =
target.transform.position;
SetDistance();
}
void Start
()
{
if (rigidbody)
{
rigidbody.freezeRotation =
true;
transform.position = target.transform.position;
}
}
//设置相机与人物之间的距离
void SetDistance()
{
transform.Translate(Vector3.forward * theDistance);
}
请问,运行时发现摄像头只是上下移动,并不实现拉近后退的效果,这是为什么? 展开
1个回答
展开全部
不是distance,是fieldOfView,把下面的代码保存为C#再赋给主相机
using UnityEngine;
using System.Collections;
public class CubeStatic : MonoBehaviour
{
public float minFov = 15f;
public float maxFov = 90f;
public float sensitivity = 10f;
void Update()
{
float fov = Camera.main.fieldOfView;
fov += Input.GetAxis("Mouse ScrollWheel") * sensitivity;
fov = Mathf.Clamp(fov, minFov, maxFov);
Camera.main.fieldOfView = fov;
}
}
using UnityEngine;
using System.Collections;
public class CubeStatic : MonoBehaviour
{
public float minFov = 15f;
public float maxFov = 90f;
public float sensitivity = 10f;
void Update()
{
float fov = Camera.main.fieldOfView;
fov += Input.GetAxis("Mouse ScrollWheel") * sensitivity;
fov = Mathf.Clamp(fov, minFov, maxFov);
Camera.main.fieldOfView = fov;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询