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);
  }
相关推荐
狂人开飞机6 小时前
14、游戏手感与视觉打磨
游戏·游戏引擎·godot
狂人开飞机12 小时前
15、音频系统
游戏引擎·godot
狂人开飞机12 小时前
16、粒子线条与视觉特效
游戏引擎·godot
心前阳光1 天前
Unity发布PC软件图标不能改变
unity·游戏引擎
an86950011 天前
unity如何在visual studio中打断点debug
unity·游戏引擎·visual studio
xcLeigh1 天前
Unity基础:使用Transform控制物体移动——Translate与position详解
unity·游戏引擎
FairGuard手游加固1 天前
Unity小游戏加密:global-metadata.dat与AssetBundle保护
游戏·unity·游戏引擎
狂人开飞机1 天前
11、UI系统
游戏引擎·godot
真鬼1231 天前
【Unity AI】Untiy链接cursor与MCP
unity·游戏引擎
WarPigs2 天前
Unity设置人物位置无效的原因
unity