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

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

相关推荐
chenzhou__7 分钟前
LinuxC语言文件i/o笔记(第十七天)
linux·c语言·笔记·学习
chenzhou__10 分钟前
LinuxC语言文件i/o笔记(第十八天)
linux·c语言·笔记·学习
01100001乄夵19 分钟前
FPGA模块架构设计完全入门指南
经验分享·笔记·学习方法·fpga入门·fpga学习之路
01100001乄夵20 分钟前
FPGA零基础入门:Verilog语法攻略
经验分享·笔记·学习方法·fpga入门·fpga学习之路
受之以蒙1 小时前
Rust ndarray 高性能计算:从元素操作到矩阵运算的优化实践
人工智能·笔记·rust
霜绛1 小时前
Unity:lua热更新(一)——AB包AssetBundle、Lua语法
笔记·学习·游戏·unity·lua
霜绛1 小时前
Unity:lua热更新(二)——Lua语法(续)
笔记·学习·unity·游戏引擎·lua
yi碗汤园1 小时前
【一文了解】C#反射
开发语言·unity·c#
HahaGiver6662 小时前
Unity Shader Graph 3D 实例 - 基础的模型贴图渲染
3d·unity·游戏程序·贴图·游戏美术
HahaGiver6663 小时前
Unity Shader Graph 3D 实例 - 一个简单的3D打印效果
3d·unity·游戏引擎