美文网首页
Unity 编写入门项目 roll a ball 之 相机跟随(

Unity 编写入门项目 roll a ball 之 相机跟随(

作者: 简栋梁 | 来源:发表于2021-08-23 00:18 被阅读0次

设置相机初始变换值,确保原点处于视口中


在 Script 目录下,新建 script 文件,并命名为 CameraController


双击打开、编辑脚本(CameraController.cs)

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

public class CameraController : MonoBehaviour
{

    public GameObject player;
    private Vector3 offset;     // position 偏移量

    // 生命周期函数
    // 触发时机:物体被加载到场景时
    void Start()
    {
        // 获取相机与球体的 position 偏移量
        offset = transform.position - player.transform.position;
    }

    // 生命周期函数
    // 触发时机:所有 Update 生命周期结束后
    void LateUpdate()
    {
        transform.position = player.transform.position + offset;
    }
}


绑定脚本至相机


绑定 GameObject 至脚本


运行项目

相关文章

网友评论

      本文标题:Unity 编写入门项目 roll a ball 之 相机跟随(

      本文链接:https://www.haomeiwen.com/subject/pkqhiltx.html