zoukankan      html  css  js  c++  java
  • Unity 简单的第三人称视角

      摄像机跟随目标移动,并在水平和垂直方向做平滑处理

     1 using UnityEngine;
     2 using System.Collections;
     3 
     4 public class ThirdControl: MonoBehaviour {
     5          public Transform target;//要跟随的目标
     6          public float distance=8.0f;//摄像机离目标的距离
     7          public float height=5.0f;//摄像机离目标的高度
     8          public float heihtDamping=0.3f;//水平跟随平滑系数
     9          public float rotationDamping=0.3f;//跟随高度变化系数
    10          public float refRotation=0f;
    11          public float refHeight=0f;
    12 
    13          void LateUpdate(){
    14                if(target){
    15                       float targetRotationAngle=target.eulerAngles.y;//目标的朝向
    16                       float targetHeight=target.position.y+height;//得到跟随的高度
    17 
    18                       float cameraRotationAngle=transform.eulerAngles.y;//摄像机的朝向
    19                       float cameraHeight=transform.position.y;//摄像机的高度
    20        
    21                       cameraRotationAngle=Mathf.SmoothDampAngle(cameraRotationAngle,targetRotationAngle,ref refRotation,rotationDamping);//从摄像机目前的角度变换到目标的角度
    22                      
    23                       cameraHeight=Mathf.SmoothDamp(cameraHeight,targetHeight,ref refHeight,heightDamping);//从摄像机目前的高度平滑变换到目标的高度
    24 
    25                        Quaternion cameraRotation=Quternion.Euler(0,cameraRotationAngle,0);//每帧在Y轴上旋转摄像机 旋转的角度为cameraRotationAngle 因为上面的代码已经得到了每帧要从摄像机当前的角度变换到目标角度cameraRotationAngle
    26 
    27                        //下面几句代码主要设置摄像机的位置
    28                        transform.position=target.position;
    29                        transform.position-=cameraRotation*Vecotr3.forward*distance;
    30                        transform.position=new Vector3(transform.position.x,cameraHeight,transform.position.z);
    31 
    32                        //使摄像机一直朝着目标方向
    33                        transform.LookAt(target);
    34                }
    35          }
    36 }
  • 相关阅读:
    [JS]格式化Date和分析格式化Date字符串的JS代码
    发布一个实用美观的Vista Sidebar Gadget (代表博客园参赛)
    2月8号N706从深圳回湖南,有没有同行的,呵呵
    LiveWriter测试
    自动内容滚动条[WinForm控件]
    关于Ajax的一揽子工程(1)
    “做事先做人”实战
    成熟度即流程
    金秋游故宫
    html块状元素、内联元素
  • 原文地址:https://www.cnblogs.com/He-Jing/p/3792690.html
Copyright © 2011-2022 走看看