猿问

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

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);
}

请问,运行时发现摄像头只是上下移动,并不实现拉近后退的效果,这是为什么?

幕布斯7119047
浏览 1125回答 1
1回答

慕哥9229398

不是distance,是fieldOfView,把下面的代码保存为C#再赋给主相机using UnityEngine;using System.Collections;public class CubeStatic : MonoBehaviour{public float minFov&nbsp;= 15f;public float maxFov&nbsp;= 90f;public float sensitivity = 10f;void Update(){float&nbsp;fov&nbsp;= Camera.main.fieldOfView;fov += Input.GetAxis("Mouse ScrollWheel") * sensitivity;fov = Mathf.Clamp(fov, minFov, maxFov);Camera.main.fieldOfView = fov;}}&nbsp;&nbsp;
随时随地看视频慕课网APP

相关分类

Unity 3D
我要回答