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

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

你可能感兴趣的:(unity 自带脚本smoothfollow的修改,实现不同方向的跟随)