Unity3D鼠标滚轮控制摄像机远近

js代码:
  //  鼠标中间键
var MouseWheelSensitivity = 5;
var MouseZoomMin = 2;
var MouseZoomMax = 10;
var normalDistance:float;
摄像机距离视角中心最近为2,最远为10,鼠标灵敏度为5
Js代码:
// 如果按住滑轮
if (Input.GetAxis("Mouse ScrollWheel") != 0)
{
//Debug.Log(Input.GetAxis("Mouse ScrollWheel"));
//Debug.Log(distance);
if (normalDistance >= MouseZoomMin && normalDistance <= MouseZoomMax)
{
normalDistance -= Input.GetAxis("Mouse ScrollWheel") * MouseWheelSensitivity;
}
if (normalDistance < MouseZoomMin)
{
normalDistance = MouseZoomMin;
}
if (normalDistance > MouseZoomMax)
{
normalDistance = MouseZoomMax;
}
}
 
 
 
第二种方法:修改第一人称的摄像机FSPTInputController
private var motor : CharacterMotor;
var speedZ=800;
// Use this for initialization
function Awake () {
 motor = GetComponent(CharacterMotor);
}
// Update is called once per frame
function Update () {
 // Get the input vector from kayboard or analog stick
 var directionVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Mouse ScrollWheel")*speedZ);
 
 if (directionVector != Vector3.zero) {
  // Get the length of the directon vector and then normalize it
  // Dividing by the length is cheaper than normalizing when we already have the length anyway
  var directionLength = directionVector.magnitude;
  directionVector = directionVector / directionLength;
  
  // Make sure the length is no bigger than 1
  directionLength = Mathf.Min(1, directionLength);
  
  // Make the input vector more sensitive towards the extremes and less sensitive in the middle
  // This makes it easier to control slow speeds when using analog sticks
  directionLength = directionLength * directionLength;
  
  // Multiply the normalized direction vector by the modified length
  directionVector = directionVector * directionLength;
 }
 
 // Apply the direction to the CharacterMotor
 motor.inputMoveDirection = transform.rotation * directionVector;
 motor.inputJump = Input.GetButton("Jump");
}
// Require a character controller to be attached to the same game object
@script RequireComponent (CharacterMotor)
@script AddComponentMenu ("Character/FPS Input Controller")

你可能感兴趣的:(U3D)