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

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

  1. Main Camera下添加组件(follow target)

  2. 脚本中定义

csharp 复制代码
public Transform trans;
  1. 将transform拖拽到trans中,让trans可以引用到transform数值(方式1)

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

csharp 复制代码
transform.position  //此方法是在父类的MonoBehaviour中定义
  1. 定义目标对象
csharp 复制代码
public GameObject player;
  1. 添加目标引用对象

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

csharp 复制代码
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;
    }
}

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

相关推荐
木白CPP10 分钟前
Persistence subsystem 笔记
数据库·笔记
味悲20 分钟前
Nginx笔记
linux·运维·笔记·nginx
坤坤子吖25 分钟前
C++继承:继承的基本用法与访问规则
开发语言·c++·笔记·学习
星马梦缘27 分钟前
JetAuto VLN语义目标探索
人工智能·数码相机
启观川28 分钟前
[windows] windows下将文件夹映射为磁盘
windows·笔记
ss27333 分钟前
AI时代首先数据库-PostgreSQL实战笔记 | 23 PostgreSQL数据库文件系统与数据页结构
数据库·笔记·postgresql
weixin_416660071 小时前
Mermaid流程图导出到Word:SVG、PNG与清晰度如何选择?
开发语言·c#
ljt27249606611 小时前
Python笔记(九)--asyncio
笔记·python
hss123456781 小时前
装修想省心,到底该从哪一步开始选
笔记
深圳老胡1 小时前
STM32 工程在 VSCode 中用 AI 搭建软件骨架架构:从零到可运行的分层实践
笔记·stm32·单片机·代码规范