为了账号安全,请及时绑定邮箱和手机立即绑定

unity3d鼠标滚轮实现摄像头拉近后退

unity3d鼠标滚轮实现摄像头拉近后退

幕布斯7119047 2019-02-12 13:08:00
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就设置为MaxDistanceif(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 回答

?
慕哥9229398

TA贡献1877条经验 获得超6个赞

不是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;
}
}

 

 


查看完整回答
反对 回复 2019-03-04
  • 1 回答
  • 0 关注
  • 1124 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信