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

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

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Mathf.Abs绝对值 计算并返回指定参数 f 绝对值。 Mathf.Acos反余弦 static functi...
    MiuLeung阅读 973评论 0 1
  • 该系列笔记基于Unity3D 5.x的版本学习,部分API使用和4.x不一致。 目前在Unity3D中,除了新的U...
    某人在阅读 2,437评论 0 2
  • 鬼剑士 “不屈服于鬼神的妨碍,一如既往地修炼自己的宝剑,这不是武道的极限吗?”有这样的一些人。无论是心脏虚弱还是缺...
    灬灬坤阅读 1,048评论 0 0
  • “舢板猛烈地摇晃着。舞女依然禁闭双唇,凝视着一个方向。我抓住绳梯,回过头去,舞女想说声再见,可话到了嘴边又咽了回去...
    漫语和流年阅读 769评论 0 4
  • 我纳闷米菲这套书这么经典这么出名,为什么我女儿不喜欢,难道书白买了?
    淇小妹阅读 310评论 0 0