当前位置: 首页 > article >正文

unity中控制相机跟随物体移动

unity中控制相机跟随物体移动

  1. Main Camera下添加组件(follow target)
    在这里插入图片描述

  2. 脚本中定义

public Transform trans;
  1. 将transform拖拽到trans中,让trans可以引用到transform数值(方式1)
    在这里插入图片描述

  2. 因为属于当前GameObject下的脚本组件,不使用拖拽的方式的话,可以直接在代码中用下面方式获取到transform的数值(方式2)

transform.position  //此方法是在父类的MonoBehaviour中定义
  1. 定义目标对象
public GameObject player;
  1. 添加目标引用对象
    在这里插入图片描述

  2. 脚本里添加相对位置的设置逻辑

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class followTarget : MonoBehaviour
{
    // public Transform trans;
    public GameObject player;
    private Vector3 offset;
    // Start is called before the first frame update
    void Start()
    {
        offset = transform.position - player.transform.position;
    }

    // Update is called once per frame
    void Update()
    {
        transform.position = player.transform.position + offset;
    }
}

到此可实现控制相机跟随物体移动!


http://www.kler.cn/a/419346.html

相关文章:

  • AI服务器从HBM到CXL的技术变革
  • 【UG\NX二次开发-Block UI】指定方位 VisibleManipulatorHandles 设置控制器手柄可见
  • 小车AI视觉交互--2.颜色跟随
  • 每日速记10道java面试题06
  • 七、Python —— 元组、集合和字典
  • windows部署PaddleSpeech详细教程
  • 解决Ubuntu下无法远程登录
  • Qt中QGraphics绘图类相关解释
  • Python PDF转JPG图片小工具
  • 深度学习-48-AI应用实战之基于face_recognition的人脸识别
  • 【AI日记】24.12.01 kaggle 比赛 Titanic-4
  • gitee:删除仓库
  • 【大模型】深度解析 NLP 模型5大评估指标及 应用案例:从 BLEU、ROUGE、PPL 到METEOR、BERTScore
  • Doris 2.1.7镜像制作
  • 【Figma】中文版安装
  • 【智能流体力学】RAG大模型方法:解决固体力学和流体动力学问题
  • 【优选算法篇】滑动窗口的艺术:如何动态调整子区间解决复杂问题(中篇)
  • 高德应用OceanBase云数据库的升级选型与迁移干货
  • MATLAB中exportgraphics函数用法
  • 【数据库设计】如何根据UI界面设计数据库结构