unity 自带脚本smoothfollow的修改,实现不同方向的跟随

实现Unity中人物控制的多角度跟随摄像机
本文详细介绍了如何在Unity环境下通过自定义脚本来实现人物控制时摄像机能够从不同角度(左、后、前)平滑跟随角色,提供了包括角度计算、高度控制、摄像机位置调整等关键代码实现细节。

////////////////2015/09/19////////////////

///////////////by  xbw//////////////////////

//////////////环境 unity 4.6.1/////////

今天弄了人物控制的,就用了unity自带的smoothfollow脚本,但是他总是显示的是人物的后背,感觉很不爽,

于是实现了三个角度跟随角色变换,代码如下;

/*
This camera smoothes out rotation around the y-axis and height.
Horizontal Distance to the target is always fixed.

There are many different ways to smooth the rotation but doing it this way gives you a lot of control over how the camera behaves.

For every of those smoothed values we calculate the wanted value and the current value.
Then we smooth it using the Lerp function.
Then we apply the smoothed values to the transform's position.
*/

// The target we are following
var target : Transform;
// The distance in the x-z plane to the target
var distance = 10.0;
// the height we want the camera to be above the target
var height = 5.0;
// How much we 
var heightDamping = 2.0;
var rotationDamping = 3.0;
var a=180;
var b=1;
// Place the script in the Camera-Control group in the component menu
@script AddComponentMenu("Camera-Control/Smooth Follow")


function OnGUI()
{
    if (GUI.Button(new Rect(Screen.width-Screen.width * 0.14f, 0, Screen.width * 0.14f, Screen.height * 0.07f), "left"))
    {
        a=90;
        b=0;
    }
    if (GUI.Button(new Rect(Screen.width-Screen.width * 0.14f,  Screen.height*0.1f, Screen.width * 0.14f, Screen.height * 0.07f), "back"))
    {
        a=0;
        b=2;
    }
    if (GUI.Button(new Rect(Screen.width-Screen.width * 0.14f, Screen.height*0.2f, Screen.width * 0.14f, Screen.height * 0.07f), "forward"))
    {
        a=180;
        b=1;
    }
}
function LateUpdate () {
	// Early out if we don't have a target
	if (!target)
		return;
	
	// Calculate the current rotation angles
	var wantedRotationAngle = target.eulerAngles.y;
	var wantedHeight = target.position.y + height;
		
	var currentRotationAngle = transform.eulerAngles.y;
	var currentHeight = transform.position.y;
	
    // Damp the rotation around the y-axis
    //修改了一下角度,使摄像机可以侧面跟随物体走动
	currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle-a, rotationDamping * Time.deltaTime);

	// Damp the height
	currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);

    // Convert the angle into a rotation
    //修改了一下角度,使摄像机可以侧面跟随物体走动
	var currentRotation = Quaternion.Euler (0, currentRotationAngle+a, 0);
	
	// Set the position of the camera on the x-z plane to:
	// distance meters behind the target
	transform.position = target.position;
    //这个left也是,与之前减去的90度相对应,这样实现了侧面跟随
    if(b==1)
        transform.position -= currentRotation * Vector3.back * distance;
    if(b==0)
        transform.position -= currentRotation * Vector3.left* distance;
    if(b==2)
        transform.position -= currentRotation * Vector3.forward* distance;
	// Set the height of the camera
	transform.position.y = currentHeight;
	
	// Always look at the target
	transform.LookAt (target);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值