unity 摄像机(Camera)跟随物体移动

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_35959554/article/details/87717031
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraFollow : MonoBehaviour
{

    public Transform playerTransform; // 移动的物体
    public Vector3 deviation; // 偏移量

    void Start()
    {
        deviation = transform.position - playerTransform.position; // 初始物体与相机的偏移量=相机的位置 - 移动物体的偏移量
    }

    void Update()
    {
        transform.position = playerTransform.position + deviation; // 相机的位置 = 移动物体的位置 + 偏移量

    }
}

直接把上述代码,赋给Camera。

猜你喜欢

转载自blog.csdn.net/weixin_35959554/article/details/87717031