• 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
  • 相关阅读:
    vmware vSphere client中,选择文件->部署OVF模板,报错处理方法
    [POJ 1521]--Entropy(哈夫曼树)
    [HDU 1016]--Prime Ring Problem(回溯)
    [HDU 2553]--N皇后问题(回溯)/N皇后问题的分析
    平面最近点对问题(分治)
    [GDUT 决赛]--GCD,LCM——我是好人(数论)
    [HDU 1428]--漫步校园(记忆化搜索)
    [Swust OJ 643]--行列式的计算(上三角行列式变换)
    [Swust OJ 491]--分数的位置(简单版)
    [Swust OJ 465]--吴奶奶买鱼(0-1背包+dfs)
  • 原文地址:https://www.cnblogs.com/ninomiya/p/6486534.html
Copyright © 2020-2023  润新知