unity跟踪摄影机

@script AddComponentMenu("Level4/Cameras/FollowCamera")
////////////////////////////////////
//跟随 摄影机
////////////////////////////////////

/**摄影机的目标*/
var target:Transform;

/**
	与目标位置的偏移向量
	y: 影响摄影机的视角.
	z: 影响摄影机与跟随目标的距离
*/
var offsetPos:Vector3 = Vector3(0,2,-4);
/**跟随target的速度*/
var followSpeed:float = 0.1;

function Update(){
	UpdateCamera();
}
/**
	更新摄影机
*/
function UpdateCamera(){
	transform.rotation = target.rotation;
	transform.Translate(offsetPos * followSpeed);
	//线性移动过去
	transform.position = Vector3.Lerp(transform.position,target.position,followSpeed);
	//直接到位
	// transform.position = target.position; 
	transform.rotation *= Quaternion.LookRotation(-offsetPos);
}


@script RequireComponent(FollowedCamera)
//////////////////////////////////////////////////////
///跟随摄影机,滚轮缩放功能
//////////////////////////////////////////////////////

/**摄影机最小偏移位置*/
var minPos:Vector3 = Vector3(0,1,-8);
/**摄影机最大偏移位置*/
var maxPos:Vector3 = Vector3(0,8,-4);
/**滚轮响应速度*/
var scrollWheelSpeed:float = 2;
/**视野 调节速度*/
var viewSpeed:float = 0.7;
//跟随摄影机的实例.
private var followedCamera:FollowedCamera;

function Awake(){
	followedCamera = GetComponent(FollowedCamera);
}

function Update () {
	UpdateView();
}

//更新视野.通过更新摄影机坐标来实现.
private function UpdateView(){
	var val:float = Input.GetAxis("Mouse ScrollWheel");
	if(val != 0){
		val *= scrollWheelSpeed;
		var yPos:float = val;
		var zPos:float = -val;
		var offsetPos:Vector3 = followedCamera.offsetPos;
		
			offsetPos += Vector3(0,yPos,zPos);
			offsetPos.y = Mathf.Clamp(offsetPos.y,minPos.y,maxPos.y);
			offsetPos.z = Mathf.Clamp(offsetPos.z,minPos.z,maxPos.z);
		
		//folloedCamera.offsetPos = offsetPos;
		followedCamera.offsetPos = Vector3.Lerp(followedCamera.offsetPos,offsetPos,viewSpeed);
	}
}

你可能感兴趣的:(followcamera)