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;
    }
}

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

相关推荐
moxiaoran57533 小时前
uni-app学习笔记二十九--数据缓存
笔记·学习·uni-app
时间之里5 小时前
【图像处理3D】:焦距的像素单位标定
图像处理·数码相机·3d
自小吃多6 小时前
STC8H系列 驱动步进电机
笔记·单片机
安木夕7 小时前
C#-Visual Studio宇宙第一IDE使用实践
前端·c#·.net
moxiaoran57538 小时前
uni-app学习笔记三十--request网络请求传参
笔记·学习·uni-app
gregmankiw10 小时前
C#调用Rust动态链接库DLL的案例
开发语言·rust·c#
XR-AI-JK10 小时前
Unity VR/MR开发-VR/开发SDK选型对比分析
unity·vr·mr
明月醉窗台11 小时前
qt使用笔记二:main.cpp详解
数据库·笔记·qt
阿蒙Amon11 小时前
06. C#入门系列【自定义类型】:从青铜到王者的进阶之路
开发语言·c#
Shaoxi Zhang12 小时前
NVM常用命令记录
笔记