美文网首页
Unity 第三人称控制器 自带追随脚本C#讲解

Unity 第三人称控制器 自带追随脚本C#讲解

作者: 老夫刘金彪啊 | 来源:发表于2015-09-18 20:05 被阅读1824次

using UnityEngine;

using System.Collections;

public class _Camera : MonoBehaviour {

private float  distance = 10.0f;//摄像机距离追随目标前后距离

private float height = 5.0f;//摄像机追随目标高度

private float heightDamping = 2.0f;//摄像机上下移动平滑速度

private float rotationDamping = 3.0f;//摄像机旋转平滑速度

public  Transform target;//追随目标

// Use this for initialization

void Start () {

}

void  LateUpdate () {

float wantedRotationAngle = target.eulerAngles.y;//追随目标 欧拉角Y

float wantedHeight = target.position.y + height;//摄像机将要处于的高度

float currentRotationAngle = transform.eulerAngles.y;//摄像机当前欧拉角Y

float currentHeight = transform.position.y;//摄像机当前高度

// Damp the rotation around the y-axis//缓慢绕Y轴改变到目标欧拉角

currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);

// Damp the height缓慢移动到目标高度

currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);

// Convert the angle into a rotation将欧拉角转换为四元数

Quaternion currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);

// Set the position of the camera on the x-z plane to:使摄像机处于XZ平面

// distance meters behind the target使摄像机处于目标后distance处

transform.position = target.position;

transform.position -= currentRotation * Vector3.forward * distance;

// Set the height of the camera设置摄像机高度

transform.position = new Vector3 (transform.position.x, currentHeight,transform.position.z);

// Always look at the target看向目标

transform.LookAt (target);

}

}

相关文章

网友评论

      本文标题:Unity 第三人称控制器 自带追随脚本C#讲解

      本文链接:https://www.haomeiwen.com/subject/lzbbcttx.html