Unity摄像机跟随

Unity摄像机跟随

方法一:摄像机子物体

将摄像机直接拖拽到被跟随的目标下面即可,这样摄像机永远在目标的后面

缺点:

  1. 屏幕旋转太平滑了
  2. 目标物体在屏幕上的位置永远不变
  3. 目标物体被销毁时总不能把摄像机也销毁了吧

方法二:子物体加向量得到摄像机位置

先相机坐标和物体坐标做差,求得偏移量,在之后的每一帧里,将偏移量加上物体的坐标。

需要注意的是,理想中的相机位置,应该是在物体本地坐标系上加上偏移量,所以我们需要将这个偏移量假设成本地坐标系下的,然后转换成世界坐标系,再进行相加

此时可以正确跟随,但是会比较僵硬,所以我们使用插值对相机现在的位置和目标位置进行插值。

最后让相机一直看向物体即可。

c# 复制代码
  public Transform player_transform;
  private Vector3 offset;
  public float smooth;
  void Start()
  {
    offset = this.transform.position - player_transform.position;
    smooth = 3;
  }

  void LateUpdate()
  {
    Vector3 target_position = player_transform.position + player_transform.TransformDirection(offset);
    transform.position = Vector3.Lerp(transform.position, target_position, Time.deltaTime * smooth);
    transform.LookAt(player_transform);
  }
相关推荐
DowneyJoy8 分钟前
【多媒体交互】透明无边框窗口
unity·c#·.net·交互
CreasyChan17 分钟前
Unity GC实战优化总结
unity·c#
Howrun7774 小时前
虚幻引擎_AController_APlayerController_AAIController
开发语言·c++·游戏引擎·虚幻
习惯就好zz5 小时前
Godot Player CharacterBody2D 移动和停止配置
游戏引擎·godot·characterbody2d·animationplayer·animationtree
ysn111116 小时前
Unity合批实战
unity·游戏引擎
Howrun7777 小时前
虚幻引擎_玩家控制器APlayerController(进阶)
游戏引擎·虚幻
IMPYLH7 小时前
Lua 的 Package 模块
java·开发语言·笔记·后端·junit·游戏引擎·lua
警醒与鞭策8 小时前
大模型对比
unity·性能优化·c#·游戏引擎·cursor
Howrun7778 小时前
虚幻引擎_玩家控制器APlayerController(初阶)
游戏引擎·虚幻
WarPigs8 小时前
switch和硬编码字典的等效性
unity·c#