zoukankan      html  css  js  c++  java
  • Unity

    相机跟随的几种方式

    1. 单纯的相机固定跟随

    相机保持与目标对象一定的相对距离,跟随目标对象发生移动

     将脚本挂载到指定的 Camera 上,并给 Target 字段赋值一个跟随的目标对象

     1 using System.Collections;
     2 using System.Collections.Generic;
     3 using UnityEngine;
     4 
     5 public class CameraFollow_A : MonoBehaviour
     6 {
     7     public Transform target; //目标对象
     8     private Vector3 offset; //距离差
     9 
    10     void Start()
    11     {
    12         offset = target.position - this.transform.position;
    13     }
    14 
    15     void Update()
    16     {
    17         this.transform.position = target.position - offset;
    18     }
    19 }

    2. 带有旋转角度的相机固定跟随

    这一种方法在第一种相机跟随的基础上,添加了相机跟随的角度

     同样挂载到指定的 Camera 上,相机将按照指定角度跟随目标对象

     可以通过调节 Speed 字段的值,调整相机跟随的平滑度

     1 using System.Collections;
     2 using System.Collections.Generic;
     3 using UnityEngine;
     4 
     5 public class CameraFollow_B : MonoBehaviour
     6 {
     7     private Transform target; //目标对象
     8     private Vector3 offset = new Vector3(0, 7, -6); //相机与目标对象的相对位置
     9     private Vector3 pos;
    10     public float speed = 2; //旋转的平滑速度
    11 
    12     void Start()
    13     {
    14         //利用标签Tag锁定所要跟随的目标对象
    15         target = GameObject.FindGameObjectWithTag("Player").transform;
    16     }
    17 
    18     void Update()
    19     {
    20         pos = target.position + offset;
    21         this.transform.position = Vector3.Lerp(this.transform.position, pos, speed * Time.deltaTime); //调整相机与目标对象的距离
    22         Quaternion angel = Quaternion.LookRotation(target.position - this.transform.position); //获取旋转角度
    23         this.transform.rotation = Quaternion.Slerp(this.transform.rotation, angel, speed * Time.deltaTime);
    24     }
    25 }

    3. 第三人称相机跟随

    相机始终以第三人称视角看向目标对象,并进行跟随

     同样将脚本挂载到指定的 Camera 上,相机将始终以第三人称视角跟随目标对象

     

    通过 DistanceUp / DistanceAway 字段调整相机角度;通过 Smooth 字段调节相机移动的平滑程度

     1 using System.Collections;
     2 using System.Collections.Generic;
     3 using UnityEngine;
     4 
     5 public class CameraFollow_C : MonoBehaviour
     6 {
     7     public Transform target; //目标对象
     8 
     9     public float distanceUp = 8f; //相机与目标对象竖直方向的距离
    10     public float distanceAway = 10f; //相机与目标对象前后方向的距离
    11     public float smooth = 10f; //移动平滑值
    12 
    13     void Update()
    14     {
    15         // 通过鼠标轴控制相机的远近
    16         if ((Input.mouseScrollDelta.y < 0 && Camera.main.fieldOfView >= 3) || 
    17             Input.mouseScrollDelta.y > 0 && Camera.main.fieldOfView <= 80)
    18         {
    19             Camera.main.fieldOfView += Input.mouseScrollDelta.y * Time.deltaTime;
    20         }
    21     }
    22 
    23     void LateUpdate()
    24     {
    25         //更新相机的位置
    26         Vector3 disPos = target.position + Vector3.up * distanceUp - target.forward * distanceAway;
    27         transform.position = Vector3.Lerp(transform.position, disPos, Time.deltaTime * smooth);
    28         //更新相机的角度
    29         transform.LookAt(target.position);
    30     }
    31 }

    4. 鼠标控制相机旋转和缩放

    通过鼠标控制相机围绕目标对象上下左右的旋转、视角的缩放

     将脚本挂载到指定的 Camera 上,给 Target 字段赋值一个目标对象

     按住鼠标左键控制相机的旋转,中键滚轮控制相机视角的缩放

     1 using System.Collections;
     2 using System.Collections.Generic;
     3 using UnityEngine;
     4 
     5 public class CameraFollow_D : MonoBehaviour
     6 {
     7     public Transform target; //目标对象
     8     private Vector3 Rotion_Transform;
     9     private new Camera camera;
    10 
    11     void Start()
    12     {
    13         camera = GetComponent<Camera>();
    14         Rotion_Transform = target.position;
    15     }
    16 
    17     void Update()
    18     {
    19         Rotate();
    20         Scale();
    21     }
    22 
    23     //相机的上下左右旋转
    24     private void Rotate()
    25     {
    26         var mouse_x = Input.GetAxis("Mouse X");//获取鼠标X轴移动
    27         var mouse_y = -Input.GetAxis("Mouse Y");//获取鼠标Y轴移动
    28         if (Input.GetKey(KeyCode.Mouse0))
    29         {
    30             transform.RotateAround(Rotion_Transform, Vector3.up, mouse_x * 5);
    31             transform.RotateAround(Rotion_Transform, transform.right, mouse_y * 5);
    32         }
    33     }
    34 
    35     //相机的视角缩放
    36     private void Scale()
    37     {
    38         if (Input.GetAxis("Mouse ScrollWheel") > 0)
    39         {
    40             transform.Translate(Vector3.forward * 1f);//速度可调  自行调整
    41         }
    42         if (Input.GetAxis("Mouse ScrollWheel") < 0)
    43         {
    44             transform.Translate(Vector3.forward * -1f);//速度可调  自行调整
    45         }
    46     }
    47 }
  • 相关阅读:
    centos7 安装 supervisor
    远程桌面管理工具
    Delphi cxpagecontrol融合窗体
    Delphi调用网页美化SQL
    合并Dev BPL教程
    delphi 各新版本特性收集
    Delphi控件备份工具
    DBX Error: Driver could not be properly initialized..解决办法
    hugo 中文目录名 在 centos 的问题
    flutter Dialog里ListView的问题
  • 原文地址:https://www.cnblogs.com/ChenZiRong1999/p/12938976.html
Copyright © 2011-2022 走看看