• unity3d之相机跟随人物


    一.第三人称视角 _1

    先设置好相机与玩家之间的角度

    给相机添加代码

     1 using UnityEngine;
     2 using System.Collections;
     3 
     4 namespace CompleteProject
     5 {
     6     public class CameraFollow : MonoBehaviour
     7     {
     8         public Transform target;            // The position that that camera will be following.
     9         public float smoothing = 5f;        // The speed with which the camera will be following.
    10 
    11 
    12         Vector3 offset;                     // The initial offset from the target.
    13 
    14 
    15         void Start ()
    16         {
    17             // Calculate the initial offset.
    18             offset = transform.position - target.position;
    19         }
    20 
    21 
    22         void FixedUpdate ()
    23         {
    24             // Create a postion the camera is aiming for based on the offset from the target.
    25             Vector3 targetCamPos = target.position + offset;
    26 
    27             // Smoothly interpolate between the camera's current position and it's target position.
    28            //相机平滑的移动到目标位置,插值
    29             transform.position = Vector3.Lerp (transform.position, targetCamPos, smoothing * Time.deltaTime);
    30         }
    31     }
    32 }
    View Code

    二、第三人称视角_2,可自己根据游戏效果调节位置

     在inspector面板修改mHeight与mDistance值即可

     1 using System.Collections;
     2 using System.Collections.Generic;
     3 using UnityEngine;
     4 
     5 public class FollowCamera : MonoBehaviour 
     6 {
     7     public Transform mTarget; //相机的跟随目标
     8     public float mHeight=8f;
     9     public float mDistance=6f;
    10     public float interpolation = 20.0f;
    11     public Space space = Space.World;
    12 
    13     // Use this for initialization
    14     void Start () 
    15     {
    16         
    17     }
    18     
    19     // Update is called once per frame
    20     void LateUpdate () 
    21     {
    22         if (mTarget)
    23         {
    24             Vector3 dest = Vector3.zero;
    25             switch (space)
    26             {
    27                 case Space.World://以世界为中心
    28                     dest = mTarget.position + Vector3.up * mHeight - Vector3.forward * mDistance;
    29                     break;
    30                 case Space.Self://玩家为中心,一般用不到可省略switch
    31                     dest = mTarget.position + mTarget.up * mHeight - mTarget.forward * mDistance;
    32                     break;
    33                 default:
    34                     break;
    35             }
    36             transform.position = Vector3.Lerp(transform.position, dest, interpolation);
    37             transform.LookAt(mTarget.position);
    38         }
    39     }
    40 }
    View Code
  • 相关阅读:
    mysql 导入excel 或 .csv
    导出Excel
    jQuery.ajax
    在web项目中配置log4j
    数据分析入门
    jdbc的配置(更新中)
    Maven中项目的启动
    Maven中的配置文件
    Maven的插件管理
    Maven的依赖管理
  • 原文地址:https://www.cnblogs.com/ninomiya/p/6486534.html
Copyright © 2020-2023  润新知