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

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

相关推荐
mudtools2 小时前
.NET驾驭Word之力:理解Word对象模型核心 (Application, Document, Range)
c#·.net
SmalBox7 小时前
【光照】Unity中的[经验模型]
unity·渲染
_落纸8 小时前
三大基础无源电子元件——电阻(R)、电感(L)、电容(C)
笔记
Alice-YUE8 小时前
【CSS学习笔记3】css特性
前端·css·笔记·html
2303_Alpha8 小时前
SpringBoot
笔记·学习
大飞pkz8 小时前
【设计模式】C#反射实现抽象工厂模式
设计模式·c#·抽象工厂模式·c#反射·c#反射实现抽象工厂模式
萘柰奈9 小时前
Unity学习----【进阶】TextMeshPro学习(三)--进阶知识点(TMP基础设置,材质球相关,两个辅助工具类)
学习·unity
Yasin Chen9 小时前
Unity UI坐标说明
ui·unity
应用市场10 小时前
无人机姿态控制系统详解与实现
游戏引擎·cocos2d
唐青枫10 小时前
从入门到进阶:C#.NET Stopwatch 计时与性能测量全攻略
c#·.net